In a programming language (Python, C#, etc) I need to determine how to calculate the angle between a line and the horizontal axis?
I think an image describes best what I want:
Given (P1x,P1y) and (P2x,P2y) what is the best way to calculate this angle? The origin is in the topleft and only the positive quadrant is used.
A formula for an angle from 0 to 2pi.
There is x=x2-x1 and y=y2-y1.The formula is working for
any value of x and y. For x=y=0 the result is undefined.
f(x,y)=pi()-pi()/2*(1+sign(x))*(1-sign(y^2))
First find the difference between the start point and the end point (here, this is more of a directed line segment, not a "line", since lines extend infinitely and don't start at a particular point).
Then calculate the angle (which runs from the positive X axis at
P1
to the positive Y axis atP1
).But
arctan
may not be ideal, because dividing the differences this way will erase the distinction needed to distinguish which quadrant the angle is in (see below). Use the following instead if your language includes anatan2
function:EDIT (Feb. 22, 2017): In general, however, calling
atan2(deltaY,deltaX)
just to get the proper angle forcos
andsin
may be inelegant. In those cases, you can often do the following instead:(deltaX, deltaY)
as a vector.deltaX
anddeltaY
by the vector's length (sqrt(deltaX*deltaX+deltaY*deltaY)
), unless the length is 0.deltaX
will now be the cosine of the angle between the vector and the horizontal axis (in the direction from the positive X to the positive Y axis atP1
).deltaY
will now be the sine of that angle.EDIT (Feb. 28, 2017): Even without normalizing
(deltaX, deltaY)
:deltaX
will tell you whether the cosine described in step 3 is positive or negative.deltaY
will tell you whether the sine described in step 4 is positive or negative.deltaX
anddeltaY
will tell you which quadrant the angle is in, in relation to the positive X axis atP1
:+deltaX
,+deltaY
: 0 to 90 degrees.-deltaX
,+deltaY
: 90 to 180 degrees.-deltaX
,-deltaY
: 180 to 270 degrees (-180 to -90 degrees).+deltaX
,-deltaY
: 270 to 360 degrees (-90 to 0 degrees).An implementation in Python using radians (provided on July 19, 2015 by Eric Leschinski, who edited my answer):
All tests pass. See https://en.wikipedia.org/wiki/Unit_circle
Sorry, but I'm pretty sure Peter's answer is wrong. Note that the y axis goes down the page (common in graphics). As such the deltaY calculation has to be reversed, or you get the wrong answer.
Consider:
gives
So if in the example above, P1 is (1,1) and P2 is (2,2) [because Y increases down the page], the code above will give 45.0 degrees for the example shown, which is wrong. Change the order of the deltaY calculation and it works properly.
Considering the exact question, putting us in a "special" coordinates system where positive axis means moving DOWN (like a screen or an interface view), you need to adapt this function like this, and negative the Y coordinates:
Example in Swift 2.0
This function gives a correct answer to the question. Answer is in radians, so the usage, to view angles in degrees, is:
matlab function:
Based on reference "Peter O".. Here is the java version