ActionScript Linear Trigger Angle

How to find the angle between two points in actionscript 3.

I have an object that animates the arrows dynamically, given the points that represent the start, elbows, and then the end point.

I need the angle of the line to rotate the arrow at the tip to indicate exactly that the current segment is being drawn

I can easily get the angle for straight corner lines by finding that one axis is 0, but the angle of any line between two points is needed.

I'm familiar with getting points for a string, for example. draw a line 100px at 47 degrees:

var length:Number = 100;
var angle:uint = 48
graphics.lineTo(Math.cos(angle) * length, Math.sin(angle) * length);

      

but i am trying to get the angle from the line:

I need

given the start point and end point of the line, what is the angle of the line.

thank you very much for any and all addictions

+2


source to share


3 answers


All propose formulas using Math.atan () , giving caveats for 0 cases in the denominator. There's a whole function in there that already does this - Math.atan2 () .



Just pass in the X and Y value and it will give you the angle. No special cases - just coordinates. As usual, the return value is in radians, from -pi to + pi.

+5


source


The formula for an angle (also called an oblique line) is as follows:

angle = Math.atan((y2 - y1) / (x2 - x1))

      

Where: (x1, y1) - coordinates of the first point (beginning of line) and (x2, y2) - coordinates of the second point (end of line)



Also note that the angle is returned in radians, so if you need to get the angle in degrees, you must multiply by 180 and divide by PI:

angleInDegrees = angle * 180/Math.PI;

      

+1


source


You are on the right track using the formula to get the end point of the line. You just need to invert the equation:

tan(theta) = opposite / adjacent

      

What gives:

angle = arctan(opposite / adjacent)

      

If opposite is height (y) and adjacent is length (x).

Be careful as this breaks when the adjacent value is zero (division by zero) and arctan will throw an exception for zero itself, so you need to catch special cases of vertical or horizontal line.

As Miki D points out, the angle is in radians, but you will probably need it in this shape to calculate the angle of the arrow.

+1


source







All Articles