finding angles 0-360

前端 未结 10 1842
清酒与你
清酒与你 2020-12-25 15:16

Need help with a math issue: i need to get the true angle from 0 degrees using x and y cordinates im using this at the moment:

Math.atan((x2-x1)/(y1-y2))/(Ma         


        
10条回答
  •  南笙
    南笙 (楼主)
    2020-12-25 15:59

    The atan function only gives half the unit circle between -pi/2 and +pi/2 (0 on x axis), there is another library function that can give the whole unit circle between -pi and + pi, atan2

    I would think you are better of using atan2 to get the right quadrant rather than branching yourself, then just scale as you have been, something like

    Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI + 180
    

    The multiply by 180 over pi is just the scale from radians to degrees as in the question (but with the division by a division simplified), the +180 makes sure its always positive i.e. 0-360 deg rather than -180 to 180 deg

提交回复
热议问题