Wait, theres only one angle between two vectors if you use a Cartesian Referential because the sign of the angle counts here.
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.
Wait, theres only one angle between two vectors if you use a Cartesian Referential because the sign of the angle counts here.
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:
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.
This is incidentally just a longwinded way of describing what I just stated before
...calculated the angle for each vector and subtracted them...
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:
1
| 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?
1 2
| 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:
1 2 3 4
| 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

So there you proved it yourself.
The angle between v1 and v2 is the angle that when rotating v1 will make it point in the direction of v2.
You can obviously rotate v1 clockwise n degrees to make it point towards v2 or 360 - n degrees counterclockwise.