7

I am trying to write a formula in ActionScript 3 that will give me var "z" (please see image below) in degrees, which I will then convert to radians.

I will already know the value of vars "x" and "y". Using trigonometry, how can I calculate the length of the hypotenuse and therefore the variable angle of var z? A solution in either AS3 or psuedocode would be very helpful. Thanks.

triangle

Neysor
  • 3,893
  • 11
  • 34
  • 66
tags2k
  • 82,117
  • 31
  • 79
  • 106

4 Answers4

6

What you need is this:

var h:Number = Math.sqrt(x*x + y*y);
var z:Number = Math.atan2(y, x);

That should give you the angle in radians, you might need to swap x/y and possibly add or remove 90 degrees but it should do the trick! (Note that you don't even need h to get z when you're using atan2)

I use multiplication instead of Math.pow() just because Math is pretty slow, you can do:

var h:Number = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));

And it should be exactly the same.

grapefrukt
  • 27,016
  • 6
  • 49
  • 73
4

z is equivalent to 180 - angle of yH. Or:

180 - arctan(x/y) //Degrees
pi - arctan(x/y) //radians

Also, if actionscript's math libraries have it, use arctan2, which takes both the x and y and deals with signs correctly.

Patrick
  • 90,362
  • 11
  • 51
  • 61
1

The angle you want is the same as the angle opposed to the one wetween y and h.

Let's call a the angle between y and h, the angle you want is actually 180 - a or PI - a depending on your unit (degrees or radians).

Now geometry tells us that:

cos(a) = y/h
sin(a) = x/h
tan(a) = x/y

Using tan(), we get:

a = arctan(x/y)

As we are looking for 180 - a, you should compute:

180 -  arctan(x/y)
Vincent Robert
  • 35,564
  • 14
  • 82
  • 119
0

What @Patrick said, also the hypotenuse is sqrt(x^2 + y^2).

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272