# Angle of 2 points according to right

Hi, I need a function(VB) or a formula for calculating the angle of 2 points according to right, in 2d. I have a standing position(X, Y) and I have a looking position(X, Y) and I want to get the angle from right to the looking position.

- Julian
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
I found the solution myself. The solution is to make a triangle with a side on 90*, then you use one of these 2 formulas(Depending on the triangle):

***
Angle = ArcTan(DifferenceX / DifferenceY)

Or

Angle = ArcTan(DifferenceY / DifferenceX)
***

and then you just add: 0, 90, 180 or 270 to the results(also depending on the triangle position)

Here is the VB function of the algorithm:

***

Function GetAngleFromPosition(StandingPositionX As Double, StandingPositionY As Double, LookingPositionX As Double, LookingPositionY As Double) As Double
If StandingPositionX < LookingPositionX Then
If StandingPositionY < LookingPositionY Then
GetAngleFromPosition = (ArcTan(((StandingPositionY - LookingPositionY) / (StandingPositionX - LookingPositionX))))
Else
GetAngleFromPosition = (ArcTan(((StandingPositionX - LookingPositionX) / (StandingPositionY - LookingPositionY)))) + 90
End If
Else
If StandingPositionY < LookingPositionY Then
GetAngleFromPosition = (ArcTan(((StandingPositionY - LookingPositionY) / (StandingPositionX - LookingPositionX)))) + 270
Else
GetAngleFromPosition = (ArcTan(((StandingPositionX - LookingPositionX) / (StandingPositionY - LookingPositionY)))) + 180
End If
End If
End Function

'You need this function to convert radians from Atn to Degrees
Function ArcTan(x As Double) As Double
ArcTan = Atn(x) * (180 / 3.14159265358979)
End Function
***

If you are searching for more formulas like this one go to my web at:

http://www.hostinganime.com/3ddev/

- Julian

Experts Exchange Solution brought to you by