1.21 gigawatts
1.21 gigawatts

Reputation: 17820

How do I get the degrees of a 360 circle where 12 o clock is 0 or 360 degrees?

I am using this JavaScript/ActionScript code to figure out the angle given two x and y values but it is incorrect:

var deltaX = 10;
var deltaY = -10;
var angleInDegrees:int = -(Math.atan2(deltaY, deltaX) * 180 / Math.PI);
trace(angleInDegrees); // 45'

The results at a different points:

clock    x    y    angle
========================
12:00    0, -10  =    90
 3:00   10,   0  =     0
 6:00    0,  10  =   -90
 9:00  -10,   0  =  -180 

I'm trying to get the angle values of the following:

clock    x    y    angle
========================
12:00    0, -10  =     0
 3:00   10,   0  =    90
 6:00    0,  10  =   180
 9:00  -10,   0  =   270 

Is there another formula I can use to get the previous values?

Update: The coordinate system maybe the issue here. When you click the mouse it sets the origin point. If you move up or left you are in the negative space. If you move right or down you are in the positive space.

Upvotes: 0

Views: 853

Answers (1)

rnevius
rnevius

Reputation: 27102

How about something like the following:

function getAngle(y, x) {
    var angle = Math.atan2(-x, -y) * 180/Math.PI - 90;
    return angle < 0 ? 360 + angle : angle;  // Ensure positive angle
}

Upvotes: 1

Related Questions