Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
90
rated 0 times [  94] [ 4]  / answers: 1 / hits: 24962  / 15 Years ago, tue, november 10, 2009, 12:00:00

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))/(Math.PI/180)


but /(Math.PI/180) limits results from -90 to 90
i need 0-360



note: I'm using the angle to indicate direction:




  • 0=up

  • 90=right

  • 135=45 degree right+down

  • 180=down

  • 270=left

  • etc


More From » javascript

 Answers
29

Math.atan limits you to the two rightmost quadrants on the unit circle. To get the full 0-360 degrees:



if x < 0 add 180 to the angle
else if y < 0 add 360 to the angle.


Your coordinate system is rotated and inverted compared to mine (and compared to convention). Positive x is to the right, positive y is up. 0 degrees is to the right (x>0, y=0, 90 degrees is up (x=0,y>0) 135 degrees is up and to the left (y>0, x=-y), etc. Where are your x- and y-axes pointing?


[#98345] Friday, November 6, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
susanajamiep

Total Points: 466
Total Questions: 113
Total Answers: 108

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
susanajamiep questions
Sun, Jun 12, 22, 00:00, 2 Years ago
Mon, Mar 7, 22, 00:00, 2 Years ago
Wed, Jun 10, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;