Hi,

I've been having some problems getting my head around a particular maths problem. I have an element in 3D space and I know the (x,y,z) coordinates of both ends of the element. For example, let's say one end of the element is at (2,3,5) and the other at (10,7,4). The element can be considered as having it's own local coordinate system (which I will denote: (x',y',z') where x' runs along the length of the element (i.e. it is the axial direction), y' is then perpendicular to x' in the vertical local direction and z' is similarly orthogonal in the local lateral direction.

What I need to do is to calculate the angles between the local (x',y',z') axes of the element and the global world (x,y,z) axes (both are right-handed). I will then be able to form the 3x3 cosine matrix for the element, where each row represents a local direction and each column represents a global direction:

x y z

x' | l_1 m_1 n_1 |

y' | l_2 m_2 n_2 |

z' | l_3 m_3 n_3 |

So, for those who are unfamiliar with the cosine matrix, l_1 = cos(angle between x' and x), m_1 = cos(angle between x' and y) etc.

By considering the element as a vector (in the example, it will be {8, 4, -1}) I am able to use the dot (inner) product to compute the angles between the element (vector) and the global (x,y,z) axes (by using the base vectors of the (x,y,z) axes). This gives me the first row of the cosine matrix : l_1, m_1, n_1

What has me stuck is how to calculate the angles between the y' and z' element axes and the global system (i.e. the second and third rows of the cosine matrix)?!

I've taken the approach that if I can generate a vector perpendicular to my original vector (element) (for example, giving me the y' axis) then I'll be able to use the cross product of these two (x' and y') to generate the final vector (giving z' axis). Then I can apply the dot product as I did with the x' vector to fill in the rest of the terms within the cosine matrix.

The key issue (which you may have spotted too!) is how to generate the y' vector, as there are an infinite number of vectors perpendicular to a given vector in 3D space.

I've tried methods (including trying to work backwards from a dot product of zero - that supports the whole "infinite number of vectors" problem!) to generate this vector, but haven't been able to find one that works (I'm checking my solutions by taking the dot product of the two vectors and seeing if it equals zero. The most sensible method (that I can see) is to rotate the vector x' by pi/2 about the z-axis (i.e. to obtain y') using the following rotation matrix:

{x,y,z}_alpha = [R_alpha]{x,y,z}

[R_alpha] = | cos(alpha) sin(alpha) 0 |

| -sin(alpha) cos(alpha) 0 |

| 0 0 1 |

However, the resulting vector doesn't dot product to zero - the '1' term in the matrix sees to that, e.g. for my x' = {8, 4, -1} example, {y'} = [R_pi/2]{x'} y' = {4,-8,-1} but <x',y'> = -1.

Hopefully that's illustrated my problem clearly. I've seen lots of information in texts and on the web about rotating a coordinate system when you know the angles of rotation, but in this case I don't - I have an element with an 3D coordinate system with arbritary orientation, and I need to know how it is orientated with repect to the global coordinate system.

I hope you can help, and if you wish to ask me anything about it then please do.

Thanks,

Jonathan