Graphics Programs Reference
In-Depth Information
Distance Between Two Points
The Pythagorean theorem enables us to easily calculate the distance between any
two points. Here's how it works. Suppose we want to know the distance between two
points (x1,y1) and (x2,y2). Referring to Figure 5.12, let dx represent the difference
between the x-axis values of the two points and let dy represent the difference between
the y-axis values of the two points. Then dx = x2 - x1 and dy = y2 - y1. The distance
between the two points forms the hypotenuse of a right triangle with dx and dy as the
other two sides.
-Y
-X
+X
(x1, y1)
dx
dy
(x2, y2)
+Y
Figure 5.12 Calculating the distance between two points
From the Pythagorean theorem we know that distance 2 = dx 2 + dy 2 or
distance = dx 2 + dy 2
In ActionScript, the square root is a defined function that we can use for just such
occasions as this. The equation for the distance expression above would be written
as shown in Figure 5.13.
distance = Math.sqrt( dx * dx + dy * dy )
Figure 5.13 Formula for the distance between two points
Search WWH ::




Custom Search