-
Hi I have two points on 2d plane and need to work out the angle that they create relative to +x axis..
This is what I have come up with so far but it's too long and I'm sure there is a better way..
x = DistX - SrcX
y = DistY - SrcY
if y>0 and x>0 then Angle = ATN(abs(y)/abs(x))
if y>0 and x<0 then Angle = ATN(abs(x)/abs(y)) + 90
if y<0 and x<0 then Angle = ATN(abs(y)/abs(x)) + 180
if y<0 and x>0 then Angle = ATN(abs(x)/abs(y)) + 270
-
-
It's becoming more and more famous :)
although Arktangent(y,x) gives you the angle from 0 to 180, it's still Arctan and won't return angles above 180.
Here's what you should use for 360 degree angles:
Code:
Function AngleByOffset(offsetX As Double, offsetY As Double) As Double
If offsetX Then
AngleByOffset = Atn(offsetY / offsetX) - (offsetX > 0) * 3.14159265358979
Else
AngleByOffset = 1.5707963267949 + (offsetY > 0) * 3.14159265358979
End If
End Function
-
Thanks
Thanks Kedaman,
Works perfectly..
-
I need to find the angle of two points on a 3d plane... maybe someone could help me there, as I am not too good in maths...
-
Well I just posted this in your other thread Sastraxi, but I may as well post it again here :) You can try the geometry tutorial at flipCode for some good geometry info. To get the angle between 2 vectors you use the dot product, that much I remember.
-
yep, pretty simple actually
cos(angle) = (a·b) / |a||b|
where a and b are the vectors.
arcus cosinus can be substituted with this formula using arcus tangent, in vb5 help files:
Arccos(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)