Reputation: 39681
I have two points, one is always at the origin (0,0), and the other can be anywhere else in the world. I'd like to find the angle between them, respective to the horizontal axis.
| 2
| /
| /
| /
| /
|/ a
---1-------------- (horizontal axis)
|
a = angle (~50 degrees, counter clockwise)
In the above I would construct a right triangle and use sohcahtoa to figure out the missing angle I want, but it gets a bit ugly when the second point is in a different quadrant like in this case:
2 |
\ |
\ |
\ |
\a|a
\|a
---1--------------
|
|
a = angle (~135, counter clockwise)
I just end up with a bunch of different cases depending on what quadrant the second point is in. I'm thinking there must be a much simpler, general solution. This is kind of like trying to find the angle between a point on the edge of a circle and its center, respective to the origin's horizontal axis.
What's a good way to do this?
Upvotes: 1
Views: 5950
Reputation: 15052
First we would like to find the equation of the straight line that connects the two points:
Let p = (x0,y0) be the second point. if x=0 than the answer is 90 deg. otherwise let m be y0/x0.
y = m(x-x0) +y0
tg^-1 (that is arctg) of m is the angle.
also note that if (x0,y0) == (0,0) than the angle is undefined
Upvotes: 0
Reputation: 85966
Most programming languages/APIs provide a function, atan2()
, which finds the angle and takes the quadrant into consideration. Just use that.
Upvotes: 8