You want to calculate the distance between two points.
Create custom Math.getDistance( )
method.
You can calculate the distance (in a straight
line) from any two points by using the Pythagorean
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:
a^{2} + b^{2} = c^{2} |
You can use this formula to calculate the distance between any two
points, where a
is the difference between the
points’ x coordinates, b
is the
difference between their y coordinates, and c
(the distance to be determined) equals the square root of
(a
^{2} +
b
^{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 ...
No credit card required