You want to calculate the distance between two points.
Math.getDistance( ) method.
You can calculate the distance (in a straight
line) from any two points by using the
theorem. The Pythagorean theorem states that in any right
triangle (a triangle in which one of the angles is 90 degrees), the
length of the
hypotenuse (the long side) is
equal to the square root of the sum of the squares of the two other
sides (referred to as the
legs of the triangle).
The Pythagorean theorem is usually written as:
|a2 + b2 = c2|
You can use this formula to calculate the distance between any two
a is the difference between the
points’ x coordinates,
b is the
difference between their y coordinates, and
(the distance to be determined) equals the square root of
2). In ActionScript,
this is written as:
var c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
How do you calculate the distance between two points using a right triangle? While it might not seem immediately obvious, you can form an imaginary right triangle using any two points in the Flash coordinate system, as shown in Figure 5-1.
Figure 5-1. The hypotenuse of a right triangle is drawn between two points to calculate the distance between the points
The hypotenuse of the imaginary triangle is formed by the line connecting the two ...