# Is my angle "look at" code correct?

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

## 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 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.

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 9
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632940
• Total Posts
3009329
• ### Who's Online (See full list)

There are no registered users currently online

×