简体   繁体   中英

How to find angle of point from x-axis independant of quadrant?

Given a point (x,y), how do I calculate the angle from the x-axis?

        |    . <- (x,y)
        |   /
        |  /
        | /
        |/
--------+---------
        |
        |

Currently I have

Math.atan(y/x) * 180 / Math.PI;

However, this does not represent all the coordinates properly. Here are the results for this in each of the quadrants (counterclockwise).

Quadrant 1: 0 -> -90
Quadrant 2: 90 -> 0
Quadrant 3: 0 -> -90
Quadrant 4: 90 -> 0

How can I write a statement that will give me the angle from the x-axis of any point such that the results for the quadrants look like this.

Quadrant 1: 0 -> 90
Quadrant 2: 90 -> 180
Quadrant 3: 180 -> 270
Quadrant 4: 270 -> 360

Basically how do I make this work in all four quadrants?

请使用Math.atan2()因为它在前两个象限中返回0-> 180,在后两个象限中返回-180-> 0。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM