How to convert x,y coordinates to an angle?

前端 未结 6 829
暗喜
暗喜 2020-12-13 06:49

Microsoft provide an excellent SVG gradient maker so IE9 can also have \"CSS3\" gradients (click Custom).

I currently utilise their logic for my Fireworks and Dreamw

6条回答
  •  無奈伤痛
    2020-12-13 07:11

    The currently accepted answer is incorrect. First of all, Math.tan is totally wrong -- I suspect Mohsen meant Math.atan and this is just a typo.

    However, as other responses to that answer state, you should really use Math.atan2(y,x) instead. The regular inverse tangent will only return values between -pi/2 and pi/2 (quadrants 1 and 4) because the input is ambiguous -- the inverse tangent has no way of knowing if the input value belongs in quadrant 1 vs 3, or 2 vs 4.

    Math.atan2, on the other hand, can use the xy values given to figure out what quadrant you're in and return the appropriate angle for any coordinates in all 4 quadrants. Then, as others have noted, you can just multiply by (180/Math.pi) to convert radians to degrees, if you need to.

提交回复
热议问题