Sign in to follow this  

Is my angle "look at" code correct?

This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just recently I updated my code that basically take two coordinates, and return the angle to point the start coordinate to the end coordinate. I'm wondering if my code is correct (it works), and/or is there a better way of doing this. (The code was wrote in Visual Basic and was wrote to support the right-triangle angle.) Public Function Angle_LookAt(ByVal StartX1 As Double, ByVal StartY1 As Double, ByVal LookAtX2 As Double, ByVal LookAtY2 As Double, Optional ByVal InRadians As Boolean = True) As Double On Error Resume Next Dim Ang As Double ' If the points are on the some X plane then ... If (StartX1 - LookAtX2) = 0 Then ' ... If the first point is lower then the look-at point then ... If StartY1 > LookAtY2 Then '(Bottom Area) Ang = (PI * 0.5) '90 Degrees (North) ' ... If the first point is higher then the look-at point then ... Else '(Top Area) Ang = (PI * 1.5) '270 Degrees (South) End If Else ' ... Get the arctangent of the slope ... Ang = Atn((StartY1 - LookAtY2) / (StartX1 - LookAtX2)) ' ... If the start point is to the right of the look-at point then ... If StartX1 > LookAtX2 Then Ang = (PI - Ang) ' ... If the start point is to the left of the look-at point then ... Else ' ... If the start point is lower then the look-at point then ... If StartY1 > LookAtY2 Then '(Right-Bottom Area) Ang = (0 - Ang) ' ... If the start point is higher then the look-at point then ... Else '(Right-Top Area) Ang = ((PI * 2) - Ang) End If End If End If ' Return the results. Angle_LookAt = IIf(InRadians = True, Ang, RadiansToDegrees(Ang)) End Function

Share this post


Link to post
Share on other sites
I don't know if VB has it, but in C you get the atan and atan2 functions. atan2 takes two arguments (basically the two sides of the triangle) and means you don't need all that work to figure which quadrant you're in.

Share this post


Link to post
Share on other sites

This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this