It’s amazon how so many people fall here.
u.v/|u|.|v| = cos(t)
Very good Geometry introduction.
Also, you can join TopCoder, 
Wrong for obvious reasons. ;D
‘cos(t) = a’ has two solutions for t.
Well, if you have one angle ang between the two vectors then you will have another angle = 360 - ang between the two vectors (obviously).
That is unless the vectors are parallel, then the two angles are the same. I find it a bit odd that this should be a problem since the web is packed with solutions and examples for exactly this. I googled and looked at hit ~100 and it described it just fine. By the way KILER has a . too much in the equation 
I already know the solution. This is just a challenge question. But i bet most of these sites point out the inner product as the solution which is not correct and will lead people to compute the wrong angle 50% of the time.
Not quite sure I follow you. Since we are talking about angle between angles then we don’t care about clockwise counterclockwise (in 3d there is no such thing anyway). So say that there is an angle = 170 between the vectors, then there would also be an angle =190, just on the other side. Which one is correct? Depends on what you are looking for.
In 2D you typically want to know the angle from a vector relative to another vector. I just used (without much pondering) brute force and calculated the angle for each vector and subtracted them (just did that in a game). Not sure if that is faster than what KILER described. If you have another faster way of finding the angle of a vector relative to another and that proves most of known techniques to be wrong, then please share!!! If what you use is not on the web, then we probably won´t be able to guess either.
I never directly work with angles.
acos(1)
-5.313886335150516808261004276581286123443877333843750081075849201e-85
Which in effect is actually supposed to be 0.
I’m sure alternatives can be used within a given context. Why don’t you just use differential equations to compute the angle if that’s what you’re really after?
There are hundreds of ways to derive the same thing via alternative methods.
Wait, theres only one angle between two vectors if you use a Cartesian Referential because the sign of the angle counts here. There’s no other way to beat around this bush. This means that in a CR we have a linear space extended with two operations between vectors called the inner product (the dot product in 2d spaces) and an the outer product (also called the cross product with 2d vectors).
[quote]In 2D you typically want to know the angle from a vector relative to another vector. I just used (without much pondering) brute force and calculated the angle for each vector and subtracted them (just did that in a game).
[/quote]
Since nobody gave the right answer heres the two solutions. The first is to use the atan2 function but it only works with 2d vectors. The atan2 function is used to obtain the angle when we now the y and x coordinate. It’s similar to use atan(y/x) but it also works for the II and III quadrants of the angle (ie if x is negative or zero).
Notice that the angle of a vector is zero when the vector is pointing towards the x-axis positive direction in a 2d CR. The angle between v1 and v2 is the angle that when rotating v1 will make it point in the direction of v2. If we don’t get the right sign for the rotation it will point in the wrong direction.
So if we have two vectors v1=(v1_x, v1_y) and v2=(v2_x, v2_y) we simply do this:
angle=atan2(v2_y, v2_x) - atan2(v1_y, v2_x) OR atan2(v2_y, v2_x) = angle + atan2(v1_y, v1_x)
It’s clear that if you add ‘angle’ to the angle of v1 you obtain the angle of v2.
For vectors of any dimension one of the steps is to obtain the inner product to get the cos of the angle. The other step is to know the sign of the angle or better yet we get a 3rd vector that will work as the axis-angle of rotation.
The solution is to obtain the absolute value of the angle = acos ( (v1 . v2) / ( |v1||v2| ) ) , then obtain the outer product u = norm ( v1 * v2 ) . We this we get the an axis-angle aa = (ux, uy, uz, angle) that can then be converted into a quaternion or an orthogonal matrix to do the rotation.
Check out this site if you want to know more:
http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
hmm… didn’t read the article yet, can you tell me if this is correct then?
(i,j) is first point, (getPosX(), getPosY()) is second point
adjacent = i - getPosX();
opposite = j - getPosY();
hypotenuse = (float)Math.hypot(adjacent, opposite);
kick_angle = (opposite > 0) ? (float)Math.acos(adjacent/hypotenuse) : (float)(2*Math.PI - Math.acos(adjacent/hypotenuse));
…so is atan2 faster then this? better maybe? As I’m a mathematician, I didn’t read any tutorial, just skeched it on paper and wrote this.
OK, we might not interpret the lingo in the same way. “The angle between vector 1 and vector 2 is -45 degrees” seems very odd to me. If you are talking about a vector relative to another vector then I agree on negative angles. It is the same as distance. If you have two apples on the table and they are 10 cm apart, and you move the right apple 20 cm to the left, would you say that the distance between the apples are -10cm??? I wouldn’t.
This is sort of interesting. As I said before, I don’t think that negative applies when you are talking about “between”, but even so you still naturally have two angles between the two vectors. Say that you have two vectors with 150 degrees between them. Rotate on vector 20 degrees away, and you have 170 degrees between them. Movie it another 20 degrees. What do you say is the angle between the two vectors?
You also prove this very nicely with your code:
This is incidentally just a longwinded way of describing what I just stated before
Just another way of saying: “If you want to know angle between two vectors, then calculate the angle of the two vectors and then calculate the difference” 
Anyway, your code will give two different answers for vectors with same angel between them:
System.out.println("angle1 = "+(Math.atan2(1, 3) - Math.atan2(-1, 3)));
Here we calculate the angle between the the two vectors, and it works just fine:
angle1 = 0.6435011087932844
If we rotate both vectors 90 deg then we should have the same angle between them, right?
System.out.println("angle1 = "+(Math.atan2(1, 3) - Math.atan2(-1, 3)));
System.out.println("angle2 = "+(Math.atan2(3, -1) - Math.atan2(3, 1)));
Cool, it still works:
angle1 = 0.6435011087932844
angle2 = 0.6435011087932845
OK, lets do this for 90, 180 and 270 degrees rotation:
System.out.println("angle1 = "+(Math.atan2(1, 3) - Math.atan2(-1, 3)));
System.out.println("angle2 = "+(Math.atan2(3, -1) - Math.atan2(3, 1)));
System.out.println("angle3 = "+(Math.atan2(-1, -3) - Math.atan2(1, -3)));
System.out.println("angle4 = "+(Math.atan2(-3, 1) - Math.atan2(-3, -1)));
Result:
angle1 = 0.6435011087932844
angle2 = 0.6435011087932845
angle3 = -5.639684198386302
angle4 = 0.6435011087932845
:o :o :o
So there you proved it yourself.
You can obviously rotate v1 clockwise n degrees to make it point towards v2 or 360 - n degrees counterclockwise.
I think that you are calculating the angle of the line segment defined by the two points rather than the angle between two vectors. I just skimmed through the code so I might be wrong… The lingo can be very confusing.
atan2() does what I think your two last lines does, just wrapped up neatly, so I would go with atan2(). No danger of division by zero either, as your code exposes.
You should always use atan2 an hypot whenever you want to get polar coordinates from rectangular coordinates (that is the angle and the radius). atan2 takes the same amount of time to compute than any other trig function. hypot is much faster than doing Math.sqrt(xx + yy).
That’s one of the biggest sources of confusion when talking about angles. When you measure the angle of a vector in a 2D Cartesian Coordinate System you are always measuring the angle relative to the x-axis unit vector.
You have a point here, the word between was an ambiguous choice of words. I need o say what vector should be taken as the base vector for the rotation.
But you have to be careful with signs here. For example -90 degrees is not the same as 90 degrees (just negating the angle won’t work). When you normalize -90 to the range of [0;360] the right answer is 360-90=270 degrees.
yeah, it’s the angle between 2 points, but isn’t that same thing? Angle stays the same…
Yeah, I will change to atan2() so I don’t get division by zero if I accidently compare 2 identical points.
I also agree on angle between [0…360> (360 exclusive, as 0 is 360)
And sorry, my code calculates absolute angle in Cartesian Coordinate System, not angle between 2 points (relative angle).
A vector is defined by a direction and magnitude. This is typically done by specifying angle (from the x-axis), and the length of the vector, or (what we are talking about here) by specifying a point in space and the vector is from origo to the point. You can naturally convert between the two using cos and sin and atan. Or as you did hypot and acos, but that is a bit of a detour I think.
What you calculated was the direction from point 1 to point 2. We are looking for the differences in the angles. So you could change your code to first calc ang1 = (0,0) to (i,j) and ang2 = (0,0) to (getPosX(), getPosY()). Then calc ang2 - ang1 and that should be the answer. But
Math.atan2(i, j) - Math.atan2(getPosX(), getPosY())
is in my opinion a more elegant solution.
I wonder why that’s yet to happen to me? Oh! I now what I’m doing. 
Sorry but the dot product is defined as above. You seem to be having trouble modelling a situation by the looks of it, you obviously don’t know what you are talking about or the solution given by reading your above posts.
[quote]I already know the solution. This is just a challenge question. But i bet most of these sites point out the inner product as the solution which is not correct and will lead people to compute the wrong angle 50% of the time.
[/quote]
And i have explained why the dot product is NOT the right choice and even pointed a site that explains it more clearly. Feel free to discuss that or don’t.
If you want to stick to doing this some other way that’s fine, however I’m sticking to my dot products and differential manifolds.
It’s obvious that I cannot tell what you are asking by the last several posts.
Why would you use the dot product and why did you ask how to calculate the dot product when it’s not what you’re after?
Are we reading the same thread here? Check out the topic of this thread and my answers. I hope that you at least accept that your first answer (the dot product) is not enough to answer the question “what is the angle between vector v1 and v2” and thus the reason of this discussion and the link i provided.
I don’t see what your differential manifold reference has to do with a problem related to an euclidean space. I’m only making this thread to help people not fall into some common pitfalls in linear algebra.
Sorry.
I’m a f’ing idiot, lets move on.