Direction (sign) of rotation/angle between two N-dimensional vectors - vector

Let's assume that I have a vector, say vectorA=[1,2,3,4]. I can rotate vectorA by some angle alpha in some direction so that it becomes vectorB=[6,7,8,9] after the rotation. Given the two vectors, vectorA and vectorB, how can I work out the angle and direction of rotation?
For the angle, that's simple enough: I can use any one of the answers to this question.
For the direction (clockwise/counter-clockwise), I am a little clueless. I know how to do it in 2 dimensions and in 3 dimensions. How do I do this for N dimensions, i.e. 4D and beyond?

Related

Distance to edge of ellipse given a vector

I've been trying to figure this out for a while and haven't found the answer.
Given:
Height of ellipse,
Width of ellipse,
Xposition of vector,
Ypostion of vector,
Direction of vector.
Find the distance to the edge of the circle
Here's a simple diagram:
Distance to the edge of a circle
I ran across this post: Calculate Point collision between a point of a given vector and the edge of a Circle But this for a circle, not an ellipse.
This is my first time posting on here. I would be very grateful for any help or pointers about this.
One fairly easy way of doing this is to represent both the ellipse and the vector in their cartesian forms
x^2/a^2 + y^2/b^2 = 1 where a & b are the lengths of the semi-major (half the width) and semi-minor (half the height) axes and the centre of the ellipse is assumed to be at (0,0)
and
y - ypos = m(x - xpos) where xpos and ypos are the position of your vector and m is the slope, the cosine of the angle (direction) it makes with the x axis.
Solve them together to get the intercept and use pythagoras to calculate the distance.
This assumes that the centre of the ellipse is at (0,0) and the axes are parallel to the x and y coordinate axes. If this is not the case then you would need a more general equation for the ellipse which is discussed in great detail here in Wikipedia.
As wierdan points out in his comment you can get 0,1 or 2 solutions.
0 if the vector starts outside the ellipse and misses it completely.
1 if the vector is a tangent to the ellipse.
2 if the vector either passes through the ellipse or it's start point is inside the ellipse.
In the case of 2 solutions 0,1 or 2 may be valid
If the vector direction points away from the ellipse then the solutions are for the reciprocal vector, the one pointing 180 degrees in the opposite direction. This may also apply to the tangent solution. So the solution(s) are not valid by your criteria.
If the start point is inside the ellipse then one solution will be for the result you want and the other for the reciprocal vector. So only one solution will be valid.
If the vector passes through the ellipse then both solutions are valid, your choice if you ignore the furthest one.

Rotate normal vector onto axis plane in 4D

The problem I need to solve is to rotate a 4-simplex given in 4D on the hyperplane with normal vector (1, 1, 1, 1) so that I can draw it in 3D. For instance I need to know the rotation for the regular one having vertices e_i (that is the coordinate vectors), and all its sub-simplices after division.
In order to understand the problem, let's go one dimension back. If you have a 3-simplex in 3D on the hyperplane with normal vector (1, 1, 1) like here (http://upload.wikimedia.org/wikipedia/commons/thumb/3/38/2D-simplex.svg/150px-2D-simplex.svg.png), one can follow the idea of Nosredna to the question
Rotate normal vector onto axis plane
It works fine in 3D, but in 4D there is no cross products, so I cannot extend this answer to my question. On the other hand using rotation matrices I managed to rotate the simplex around the x axes by -45 degree, then rotating around the y axes by around 35 degree (atan(sqrt(2)/2) using the coordinate rotation matrices (http:// upload.wikimedia.org/math/2/8/5/2851c9dc2031127e6dacfb84b96446d8.png).
I also tried to calculate a rotation matrix from axes rotations like in http://ken-soft.com/2009/01/08/graph4d-rotation4d-project-to-2d/ but I could not find out what should be the angles to use. So I used R=rotXU*rotYU*rotZU with the angles pi/4, -atan(sqrt(2)/2, and -pi/6, which looked good, but somehow the result wasn't ok.
Sorry, I could not put the images directly as I'm a newbie...
Thank you for any answer!
There are no rotation axes in 4D, for the same reason there is no cross product: the group of 4D rotations is 6-dimentional, and the space you are rotating is 4-dimentional. Imagine, for example, a simultaneous rotation in XY plane and ZT plane: it has no non-zero stationary vectors, and therefore no axes.
The most appropriate thing to do is to work with the usual transormation matrix, which is applicable in any dimention N:
[ a11 ... a1N d1 ]
...
[ aN1 ... aNN dN ]
[ 0 ... 0 1 ]
Here d1 ... dN represent translations, and the NxN submatrix aIJ represent rotations, dilations, projections, and mirroring. To limit to rotations only make this matrix orthogonal: its product with its own transpose should be the unit matrix. This is a common practice for N=2 and N=3, you just do the same for N=4.
To find the appropriate rotation matrix in your case write down add the requirement that the entire 4th row of an orthogonal 4x4 matrix is zero, and that would give you a bunch of solutions, each being an acceptable answer to your question.

The X angle between two 3D vectors?

I have two 3D vectors called A and B that both only have a 3D position. I know how to find the angle along the unit circle ranging from 0-360 degrees with the atan2 function by doing:
EDIT: (my atan2 function made no sense, now it should find the "y-angle" between 2 vectors):
toDegrees(atan2(A.x-B.x,A.z-B.z))+180
But that gives me the Y angle between the 2 vectors.
I need to find the X angle between them. It has to do with using the x, y and z position values. Not the x and z only, because that gives the Y angle between the two vectors.
I need the X angle, I know it sounds vague but I don't know how to explain. Maybe for example you have a camera in 3D space, if you look up or down than you rotate the x-axis. But now I need to get the "up/down" angle between the 2 vectors. If I rotate that 3D camera along the y-axis, the x-axis doens't change. So with the 2 vectors, no matter what the "y-angle" is between them, the x-angle between the 2 vectors wil stay the same if y-angle changes because it's the "up/down" angle, like in the camara.
Please help? I just need a line of math/pseudocode, or explanation. :)
atan2(crossproduct.length,scalarproduct)
The reason for using atan2 instead of arccos or arcsin is accuracy. arccos behaves very badly close to 0 degrees. Small computation errors in argument will lead to disproportionally big errors in result. arcsin has same problem close to 90 degrees.
Computing the altitude angle
OK, it might be I finally understood your comment below about the result being independent of the y angle, and about how it relates to the two vectors. It seems you are not really interested in two vectors and the angle between these two, but instead you're interested in the difference vector and the angle that one forms against the horizontal plane. In a horizontal coordinate system (often used in astronomy), that angle would be called “altitude” or “elevation”, as opposed to the “azimuth” you compute with the formula in your (edited) question. “altitude” closely relates to the “tilt” of your camera, whereas “azimuth” relates to “panning”.
We still have a 2D problem. One coordinate of the 2D vector is the y coordinate of the difference vector. The other coordinate is the length of the vector after projecting it on the horizontal plane, i.e. sqrt(x*x + z*z). The final solution would be
x = A.x - B.x
y = A.y - B.y
z = A.z - B.z
alt = toDegrees(atan2(y, sqrt(x*x + z*z)))
az = toDegrees(atan2(-x, -z))
The order (A - B as opposed to B - A) was chosen such that “A above B” yields a positive y and therefore a positive altitude, in accordance with your comment below. The minus signs in the azimuth computation above should replace the + 180 in the code from your question, except that the range now is [-180, 180] instead of your [0, 360]. Just to give you an alternative, choose whichever you prefer. In effect you compute the azimuth of B - A either way. The fact that you use a different order for these two angles might be somewhat confusing, so think about whether this really is what you want, or whether you want to reverse the sign of the altitude or change the azimuth by 180°.
Orthogonal projection
For reference, I'll include my original answer below, for those who are actually looking for the angle of rotation around some fixed x axis, the way the original question suggested.
If this x angle you mention in your question is indeed the angle of rotation around the x axis, as the camera example suggests, then you might want to think about it this way: set the x coordinate to zero, and you will end up with 2D vectors in the y-z plane. You can think of this as an orthogonal projection onto said plain. Now you are back to a 2D problem and can tackle it there.
Personally I'd simply call atan2 twice, once for each vector, and subtract the resulting angles:
toDegrees(atan2(A.z, A.y) - atan2(B.z, B.y))
The x=0 is implicit in the above formula simply because I only operate on y and z.
I haven't fully understood the logic behind your single atan2 call yet, but the fact that I have to think about it this long indicates that I wouldn't want to maintain it, at least not without a good explanatory comment.
I hope I understood your question correctly, and this is the thing you're looking for.
Just like 2D Vectors , you calculate their angle by solving cos of their Dot Product
You don't need atan, you always go for the dot product since its a fundamental operation of vectors and then use acos to get the angle.
double angleInDegrees = acos ( cos(theta) ) * 180.0 / PI;

How do you calculate the reflex angle given two vectors in 3D space?

I want to calculate the angle between two vectors a and b. Lets assume these are at the origin. This can be done with
theta = arccos(a . b / |a| * |b|)
However arccos gives you the angle in [0, pi], i.e. it will never give you an angle greater than 180 degrees, which is what I want. So how do you find out when the vectors have gone past the 180 degree mark? In 2D I would simply let the sign of the y-component on one of the vectors determine what quadrant the vector is in. But what is the easiest way to do it in 3D?
EDIT: I wanted to keep the question general but here we go. I'm programming this in c and the code I use to get the angle is theta = acos(dot(a, b)/mag(a)*mag(b)) so how would you programmatically determine the orientation?
This works in 2D because you have a plane defined in which you define the rotation.
If you want to do this in 3D, there is no such implicit 2D plane. You could transform your 3D coordinates to a 2D plane going through all three points, and do your calculation inside this plane.
But, there are of course two possible orientations for the plane, and that will affect which angles will be > 180 or smaller.
I came up with the following solution that takes advantage of the direction change of the cross product of the two vectors:
Make a vector n = a X b and normalize it. This vector is normal to the plane spanned by a and b.
Whenever a new angle is calculated compare it with the old normal. In the comparison, treat the old and the current normals as points and compute the distance between them. If this distance is 2 the normal (i.e. the cross product a X b has flipped).
You might want to have a threshold for the distance as the distance after a flip might be shorter than 2, depending on how the vectors a and b are oriented and how often you update the angle.
One solution that you could use:
What you effectively need to do is create a plane that one of the vectors is coplanar to.
Getting the cross product of both vectors will create a plane, then is you get the normal of this plane, you can get the angle between this and the vector you need to get the signed angle for, and you can use the angle to determine the sign.
If the angle is greater than 90 degrees, then it is below the created plane; less than 90 degrees, and it is above.
Depending on cost of calculations, the dot product can be used at this stage instead of the angle.
Just make sure that you always calculate the normals by the same order of vectors.
This is useable more easily if you're using the XYZ axes, and that's what you're comparing against, since you already have the vectors needed for the plane.
There are possbly more efficient solutions, but this is one I came up with.
Edit: clarification of created vectors
a X b = p. This is perpendicular to both a and b.
Then, do either:
a X p or b X p to create another vector that is the normal to the plane created by the 2 vectors. Choice of vector depends on which you're trying to find the angle for.
Strictly speaking, two 3D vectors always have two angles between them - one below or equal to 180, the other over or equal to 180. Arccos gives you one of them, you can get the other by subtracting from 360. Think of it that way: imagine two lines intersect. You have 4 angles there - 2 of one value, 2 of another. What's the angle between the lines? No single answer. Same here. Without some kind of extra criteria, you can not, in theory, tell which of the two angle values should be taken into account.
EDIT: So what you really need is an arbitrary example of fixing an orientation. Here's one: we look from the positive Z direction. If the plane between the two vectors contains the Z axis, we look from the positive Y direction. If the plane is YZ, we look from the positive X direction. I'll think how to express this in coordinate form, then edit again.

How can I find the direction of a Vector2 with only the X and Y coordinates?

I would like to know 2 things about the struct Vector2 in XNA:
Why does this struct only have X and Y instead of X,Y (origin) and X',Y' (destination)?
How can I calculate the direction of a vector with only the X,Y?
Thanks a lot in advance.
Kind Regards.
Josema.
The origin is usually assumed to be (0,0).
The X and Y are not actually the coordinates of a point.
They are X-axis and Y-axis components of the vector. A vector by definition has no origin, it represents only direction and length, not position.
Mathematically a vector has orientation (direction) and magnitude (length). It does not have position. When vectors are used in graphics programming to represent positions they are implicitly representing a point as an offset from the origin.
If you want to convert from a vector to an angle you can use simple trigonometry - the x and y components form two sides of a triangle and you can calculate the angle the vector makes with any axis. If you want to find the angle between two arbitrary vectors a and b it's acos(dot(a, b) / (length(a) * length(b)).
To calculate the direction (angle) of a vector most languages have an atan2(y,x) function.
1) A vector does not need length.
2) The numbers themselves determine the direction of the vector. Think of the cartesian plane. If you have a negative x and a positive y, then you are going top left...positive x, positive y, top right...etc. etc.

Resources