0

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.

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • That's an error. I've fixed it. It should be 10, 0. – 1.21 gigawatts Jun 06 '15 at 09:23
  • possible duplicate of [How to use atan2() in combination with other Radian angle systems](http://stackoverflow.com/questions/17574424/how-to-use-atan2-in-combination-with-other-radian-angle-systems) – Qantas 94 Heavy Jun 06 '15 at 09:23
  • That other question does not look correct. It does not give me the results that I've described in my question (or it has but it is unclear). Please leave this question open. – 1.21 gigawatts Jun 06 '15 at 09:27
  • I'm not understanding your coordinate system. Even if 12:00 was 0 degrees...It's at the top of a clock (i.e. positive y-coordinate). Unless you're both rotating and flipping the clock. – rnevius Jun 06 '15 at 09:31
  • The coordinate system is rotated as you describe. When you click the mouse if you move up you are in the negative and if you move left you are in the negative. If you move right or down you are in the positive. – 1.21 gigawatts Jun 06 '15 at 09:37
  • @1.21gigawatts it *should* deliver the correct results, unless I've missed something. Note that you'll need to convert the result to degrees and make sure to add 360 degrees if the result is less than zero. – Qantas 94 Heavy Jun 06 '15 at 09:41

1 Answers1

1

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
}
rnevius
  • 26,578
  • 10
  • 58
  • 86