Connecting Tech Pros Worldwide Help | Site Map

How to calculate angle of a line

Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#1: Oct 17 '07
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.
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#2: Oct 17 '07

re: How to calculate angle of a line


Trignometry :)
you know the cordinates of the points,
Get the height and the width of the that line and use tan

angle = tan inverse ( X / Y)
Attached Thumbnails
tan.jpg  
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#3: Oct 17 '07

re: How to calculate angle of a line


Quote:

Originally Posted by Shashi Sadasivan

Trignometry :)
you know the cordinates of the points,
Get the height and the width of the that line and use tan

angle = tan inverse ( X / Y)

Thanks. Haven't done a lot of trig for a few years.

I had already worked out the horizontal and vertical distance (X and Y), and I routinely use these to calculate the straight-line distance (good old Pythagorus), but didn't know how to work out the angle.

I'm still not sure. I need to be able to go the full circle. That is, angles all the way around to 360 degrees (or just under, since that would be the same as zero of course). Will this work? Not sure what you mean by "inverse", in this context. (Be gentle, it has been a long time...)
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#4: Oct 18 '07

re: How to calculate angle of a line


Quote:

Originally Posted by Killer42

I'm still not sure. I need to be able to go the full circle. That is, angles all the way around to 360 degrees (or just under, since that would be the same as zero of course). Will this work?

Depends on your frame of reference (my frame of reference is the x axis)

Quote:

Originally Posted by Killer42

Not sure what you mean by "inverse", in this context. (Be gentle, it has been a long time...)

Well... lets assume that we now see @ as theta (the general angle variable)
tan @ = X/Y;
so @ = tan inverse (X/Y)
i have to type in inverse, because i cant type in -1 in superscript :D

Hope the math comes along well for you :D

cheers
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#5: Oct 18 '07

re: How to calculate angle of a line


Quote:

Originally Posted by Killer42

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.

Here's how I'd do it:
Expand|Select|Wrap|Line Numbers
  1. >>> from math import atan, degrees
  2. >>> bottom, left = 0, 0
  3. >>> top, right = 10.0, 20.0
  4. >>> degs = degrees(atan((top - bottom)/(right - left)))
  5. >>> print degs
  6. 26.5650511771
  7. >>> 
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#6: Oct 18 '07

re: How to calculate angle of a line


Expand|Select|Wrap|Line Numbers
  1. Double radians = Math.Atan((x2-x1)/(y2-y1));
  2. Double degrees = radians *  180 / Math.PI;
Thats c# :D
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#7: Oct 18 '07

re: How to calculate angle of a line


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.
  • Adjust the angle based on the quadrant.
    • 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.
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#8: Oct 18 '07

re: How to calculate angle of a line


Well...I would say calculate the degrees,
the values of X=(x2-x1) and Y=(y2-y1) will indicate which quadrant the angle is being measured to.


Assume that the quadrants are being split as in picture.
X(sign)__Y(sign) ___ Quadrant
__+_______+________ 0
__+_______- ________1
__-_______-_________2
__-_______+_________3

(Excuse the undescores, as that gets trimmed off in the posts)

The angle is measured always to the X axis, so then apply the required arithmetic to get the desired angle from your frame of ref, and angle of ref (ie, whether clockwise or anti-clockwise).
Attached Thumbnails
quadrants.jpg  
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#9: Oct 18 '07

re: How to calculate angle of a line


Thanks Shashi. That's pretty much what I'm doing. I've just complicated it a little by using the absolute values of X and Y to begin with. I plan to change that.
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#10: Oct 18 '07

re: How to calculate angle of a line


I would suggest to keep the values of X and Y as they are.
the angles yould come as positive or negative depending which quadrant there are in (0 and 2 will be positive and 1,3 negative)

Soall you then need to find which qadrant they are in
and then
(90 * (quadrant number +1)) + angle = resulting required angle

the resulting angle starts from the X axis at quadrant 0, and works it was anti clock wise from there.

cheers :) (woo hoo,, i never knew i could start liking trignometry....no...wait, i started avoiding it when my games designer friend came to ask me doubts :P)
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#11: Oct 18 '07

re: How to calculate angle of a line


Ok, thanks for that.

One thing, though. This is something which keeps cropping up here lately. The word is "questions", not "doubts".
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#12: Oct 18 '07

re: How to calculate angle of a line


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)
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#13: Oct 18 '07

re: How to calculate angle of a line


Quote:

Originally Posted by Killer42

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.
  • Adjust the angle based on the quadrant.
    • 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.  
  2. def QuadHeading(east, north):
  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.  
  7. def QuadToCirc(heading):
  8.     """Convert quadrant to degrees of a circle."""
  9.     return (heading, abs(heading + 360))[heading < 0] # index the list on bool
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#14: Oct 19 '07

re: How to calculate angle of a line


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
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#15: Oct 19 '07

re: How to calculate angle of a line


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
Newbie
 
Join Date: Oct 2007
Posts: 2
#16: Oct 20 '07

re: How to calculate angle of a line


Quote:

Originally Posted by Killer42

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!!!
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#17: Oct 20 '07

re: How to calculate angle of a line


Quote:

Originally Posted by hopalongcassidy

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

Um... isn't that what I've got?

Quote:

Originally Posted by hopalongcassidy

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