actionscript-3trigonometryhypotenuse

Triangle Trigonometry (ActionScript 3)


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


Solution

  • 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.