How to calculate angle of a line | Moderator | | Join Date: Oct 2006 Location: Australia
Posts: 7,748
| | |
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.
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | 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)
| | Moderator | | Join Date: Oct 2006 Location: Australia
Posts: 7,748
| | | 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...)
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | 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
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | 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: -
>>> from math import atan, degrees
-
>>> bottom, left = 0, 0
-
>>> top, right = 10.0, 20.0
-
>>> degs = degrees(atan((top - bottom)/(right - left)))
-
>>> print degs
-
26.5650511771
-
>>>
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | re: How to calculate angle of a line - Double radians = Math.Atan((x2-x1)/(y2-y1));
-
Double degrees = radians * 180 / Math.PI;
Thats c# :D
| | Moderator | | Join Date: Oct 2006 Location: Australia
Posts: 7,748
| | | 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.
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | 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).
| | Moderator | | Join Date: Oct 2006 Location: Australia
Posts: 7,748
| | | 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.
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | 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
| | | 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".
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | 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)
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | 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: -
-
def QuadHeading(east, north):
-
"""Given the vector, convert to degrees in a quadrant.
-
N is zero, S is 180, E is positive, W is negitive."""
-
return degrees(atan2(east, north))
-
-
def QuadToCirc(heading):
-
"""Convert quadrant to degrees of a circle."""
-
return (heading, abs(heading + 360))[heading < 0] # index the list on bool
| | Moderator | | Join Date: Oct 2006 Location: Australia
Posts: 7,748
| | | 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. - Option Explicit
-
DefLng A-Z
-
-
Private DistanceX As Single, DistanceY As Single
-
Private Const Zero As Long = 0
-
Private Const NotQuiteZero As Single = 0.00001
-
'Private Const One As Long = 1
-
'Private Const Two As Long = 2
-
'Private Const Three As Long = 3
-
Private Const Ninety As Long = 90
-
Private Const TwoSeventy As Long = 270
-
Private Const Pi As Single = 3.14159265358979
-
Private Const RadsToDegs As Single = 180 / Pi
-
-
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
-
' Given two points, return the angle of the line from P1 to P2.
-
DistanceX = To_X - From_X
-
DistanceY = To_Y - From_Y
-
If DistanceY = Zero Then DistanceY = NotQuiteZero ' Prevent div-by-zero error.
-
If DistanceY < Zero Then
-
DirectionOfLine = Ninety + Atn(DistanceX / DistanceY) * RadsToDegs
-
Else
-
DirectionOfLine = TwoSeventy + Atn(DistanceX / DistanceY) * RadsToDegs
-
End If
-
End Function
| | Moderator | | Join Date: Oct 2006 Location: Australia
Posts: 7,748
| | | re: How to calculate angle of a line
Actually, here's a shorter (and probably very slightly faster) version... - Option Explicit
-
DefLng A-Z
-
-
Private DistanceX As Single, DistanceY As Single
-
Private Const NotQuiteZero As Single = 0.00001
-
Private Const Ninety As Long = 90
-
Private Const TwoSeventy As Long = 270
-
Private Const RadsToDegs As Single = 57.29578
-
-
-
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
-
' Given two points, return the angle of the line from P1 to P2.
-
If From_Y = To_Y Then To_Y = From_Y - NotQuiteZero ' Prevent div-by-zero error.
-
If To_Y < From_Y Then
-
DirectionOfLine2 = Ninety + Atn((To_X - From_X) / (To_Y - From_Y)) * RadsToDegs
-
Else
-
DirectionOfLine2 = TwoSeventy + Atn((To_X - From_X) / (To_Y - From_Y)) * RadsToDegs
-
End If
-
End Function
| | Newbie | | Join Date: Oct 2007
Posts: 2
| | | 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
| | | 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.
|  | Similar Software Development bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|