473,883 Members | 1,653 Online

# How to calculate angle of a line

8,435 Recognized Expert Expert
Hi all.

Sorry to be brief, but have to leave.

If I have the coordinates of two points (on the computer screen), how do I calculate the angle of the line which joins them?

I know, my terms are vague and in some cases completely wrong (for instance it would be an "interval", not a line) but hopefully you get the idea.
Oct 17 '07
19 36463
Killer42
8,435 Recognized Expert Expert
Ok, thanks for that.

One thing, though. This is something which keeps cropping up here lately. The word is "questions" , not "doubts".
Oct 18 '07 #11
1,435 Recognized Expert Top Contributor
Oh, no it was doubts,
Because he was good at geometry and trignometry, and he would come and pile everything up on me, and explaining the humungus geometry, seemed that he would almost recreate the entire earth in detail.
And expected me to follow him, and call out when the error is occouring !!

so doubts...not questions !!! (questions would have been easier than doubts)
Oct 18 '07 #12
bartonc
6,596 Recognized Expert Expert
Thanks for that, people.

Since I need to cover the full circle (in other words, the angle could be up to 360 degrees), I've had to do some real kludges. Hope someone can point out a better way.
• Calculate (absolute) DistanceX & DistanceY
• Calculate Ratio: DistanceX / DistanceY
• Calculate Degrees = Atn(Ratio) * RadsToDegs
This provides a value between 0 and 90, which is not good enough. So...
• Determine which "quadrant" we're in (lower-right = 0, upper-right = 1, upper left = 2, lower left = 3) by checking signs of horizontal & vertical distances.
• Quadrant 0: 360 - Degrees
• Quadrant 1: 90 - Degrees
• Quadrant 2: 90 + Degrees
• Quadrant 3: 270 - Degrees
• And that's it. Simple, huh. :)
This does produce what looks like the right answer, or close enough. But as you can see, it's rather messy so far.

Note, using the absolute values of the distances dates back to before you people provided the Atan solution, so I'll be revisitng that to see what I can do better.
I developed these for my Garmin GPS interface:
Expand|Select|Wrap|Line Numbers
1.
3.     """Given the vector, convert to degrees in a quadrant.
4.        N is zero, S is 180, E is positive, W is negitive."""
5.     return degrees(atan2(east, north))
6.
8.     """Convert quadrant to degrees of a circle."""
Oct 18 '07 #13
Killer42
8,435 Recognized Expert Expert
For future reference, here's the function I ended up putting together in VB6. You'll note that I've defined named constants for everything rather than hard-coding any values. This is based on the fact that constants have traditionally been faster to reference than variables. I don't actually know whether this is still the case in VB6.

Expand|Select|Wrap|Line Numbers
1. Option Explicit
2. DefLng A-Z
3.
4. Private DistanceX As Single, DistanceY As Single
5. Private Const Zero As Long = 0
6. Private Const NotQuiteZero As Single = 0.00001
7. 'Private Const One As Long = 1
8. 'Private Const Two As Long = 2
9. 'Private Const Three As Long = 3
10. Private Const Ninety As Long = 90
11. Private Const TwoSeventy As Long = 270
12. Private Const Pi As Single = 3.14159265358979
13. Private Const RadsToDegs As Single = 180 / Pi
14.
15. Public Function DirectionOfLine(ByVal From_X As Single, ByVal From_Y As Single, ByVal To_X As Single, ByVal To_Y As Single) As Single
16.   ' Given two points, return the angle of the line from P1 to P2.
17.   DistanceX = To_X - From_X
18.   DistanceY = To_Y - From_Y
19.   If DistanceY = Zero Then DistanceY = NotQuiteZero ' Prevent div-by-zero error.
20.   If DistanceY < Zero Then
21.     DirectionOfLine = Ninety + Atn(DistanceX / DistanceY) * RadsToDegs
22.   Else
23.     DirectionOfLine = TwoSeventy + Atn(DistanceX / DistanceY) * RadsToDegs
24.   End If
25. End Function
Oct 19 '07 #14
Killer42
8,435 Recognized Expert Expert
Actually, here's a shorter (and probably very slightly faster) version...
Expand|Select|Wrap|Line Numbers
1. Option Explicit
2. DefLng A-Z
3.
4. Private DistanceX As Single, DistanceY As Single
5. Private Const NotQuiteZero As Single = 0.00001
6. Private Const Ninety As Long = 90
7. Private Const TwoSeventy As Long = 270
8. Private Const RadsToDegs As Single = 57.29578
9.
10.
11. Public Function DirectionOfLine2(ByVal From_X As Single, ByVal From_Y As Single, ByVal To_X As Single, ByVal To_Y As Single) As Single
12.   ' Given two points, return the angle of the line from P1 to P2.
13.   If From_Y = To_Y Then To_Y = From_Y - NotQuiteZero ' Prevent div-by-zero error.
14.   If To_Y < From_Y Then
15.     DirectionOfLine2 = Ninety + Atn((To_X - From_X) / (To_Y - From_Y)) * RadsToDegs
16.   Else
17.     DirectionOfLine2 = TwoSeventy + Atn((To_X - From_X) / (To_Y - From_Y)) * RadsToDegs
18.   End If
19. End Function
Oct 19 '07 #15
hopalongcassidy
2 New Member
Hi all.

Sorry to be brief, but have to leave.

If I have the coordinates of two points (on the computer screen), how do I calculate the angle of the line which joins them?

I know, my terms are vague and in some cases completely wrong (for instance it would be an "interval", not a line) but hopefully you get the idea.

Try arctan((y2-y1)/(x2-x1))

By the way, I read all of the replies and every one of them is wrong. The tangent of an angle is the change in Y divided by the change in X. The change in X divided by the change in Y is the cotangent!!!
Oct 19 '07 #16
Killer42
8,435 Recognized Expert Expert
Try arctan((y2-y1)/(x2-x1))
Um... isn't that what I've got?

By the way, I read all of the replies and every one of them is wrong. The tangent of an angle is the change in Y divided by the change in X. The change in X divided by the change in Y is the cotangent!!!
Thanks for the correction. I have to admit though, all I'm really concerned about is whether the code works. Which it does.
Oct 20 '07 #17
cowpie
1 New Member
Since this is the first link that pops up on Google when I search how to do this, I thought I'd include a much easier method.

Expand|Select|Wrap|Line Numbers
1. Double radians = Math.Atan2((y2-y1), (x2-x1));
2. Double degrees = radians * 180 / Math.PI;
3.
4. if(degrees < 0) {
5.    degrees += 360; // degrees are now 0-360
6. }
Aug 20 '11 #18
Verrazano
1 New Member
recently today my friend and I worked this out originally I thought of using the four quadrants of a graph to get the angle, but the way I had done it ended with me having to build a large table to get values back.
so after about an hour we came up with this:
cos^-1(adjacent/hypotenuse) this translates to being: let one point be your origin and the other point be the x and y values you get the hypotenuse by obviously using the Pythagorean theorem. So lets say the coordinates are (0,0) and (4,4) using the formula above it would look like this.
cos^-1(4/sqrt(4^2+4^2)) = 45 degrees which is correct since the slope of the line is 1/1.
hope this helps,
Verrazano
Nov 22 '11 #19
Killer42
8,435 Recognized Expert Expert
Cowpie, what language is that? VB.Net?

I am/was working in VB6. By the way, this question is from 4 years ago. :-)
Dec 13 '11 #20