Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 563
tags2k Avatar asked Sep 11 '08 09:09

tags2k


3 Answers

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.

like image 65
grapefrukt Avatar answered Nov 13 '22 01:11

grapefrukt


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.

like image 43
Patrick Avatar answered Nov 13 '22 00:11

Patrick


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)
like image 38
Vincent Robert Avatar answered Nov 13 '22 01:11

Vincent Robert