473,883 Members | 1,680 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 #1
19 36463
1,435 Recognized Expert Top Contributor
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 Images
 tan.jpg (5.4 KB, 7479 views)
Oct 17 '07 #2
Killer42
8,435 Recognized Expert Expert
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...)
Oct 17 '07 #3
1,435 Recognized Expert Top Contributor
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)

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
Oct 17 '07 #4
bartonc
6,596 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.
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. >>>
Oct 17 '07 #5
1,435 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
2. Double degrees = radians *  180 / Math.PI;
Thats c# :D
Oct 18 '07 #6
Killer42
8,435 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.
Oct 18 '07 #7
1,435 Recognized Expert Top Contributor
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.
__+_______+____ ____ 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 Images
Oct 18 '07 #8
Killer42
8,435 Recognized Expert Expert
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.
Oct 18 '07 #9
1,435 Recognized Expert Top Contributor
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)
Oct 18 '07 #10