views:

159

answers:

2

Input: two multidimensional (for example dim=8) vectors a and b.

I need to find out the "directed" angle (0-2*Pi, not 0-Pi) between those vectors a and b. And if they are not parallel I need to rotate vector b in plane a,b by "directed" angle L. If they are parallel, plane does not matter, but angle of rotation is still the same L.

For 2d and 3d this is quite easy, but for more dimensions I am lost, I didn't find anything on google, and I prefer using some already proved&tested equations (avoiding errors introduced by my calculations :-D).

Thank you in advance for tips, links, etc.

+1  A: 

I believe you should work on the plane generated by your vectors a and b. The code will then be the same regardless of the dimension (btw, the dimension of the vectors is by definition the dimension of the space).

You can do that by orthogonalizing (a,b) as:

a' = a/||a||
b1 = b - (a'·b)a'  <-- scalar product denoted by ·
b' = b1/||b||

Now you are on a plane with an orthonomal basis and should be back in business. The coordinates of b in that basis is (a'·b,b'·b). For a it is similarly (||a||,0). When you want to go back to the ambient space, simply write your vector with coordinates (x1,x2) as x1 a' + x2 b'.

I hope the math notation is not too confusing.

Olivier
Thank you! I started from rotating entire plane to XY axis with any other coordinate = 0, but I didn't think about expressing those vectors in terms of new base.
macias
+1  A: 

You may find this paper useful: Rotations for N-Dimensional Graphics by AJ Hanson. There's also this paper: General n-Dimensional Rotations. You can also check out this forum thread where a bunch of people try to work it out. And here's yet another paper: On the Rigid Rotation Concept in n-Dimensional Spaces. Must. Stop. Googling.

brainjam
Thank you very much. Great lesson on using google, I searched for "multidimensional" not "n-dimensional" :-)
macias