Archive
This is an archived tutorial about Flash. It's kept for history and may reference outdated tech. For a modern take, see Measuring the Distance Between Two Points by using the Pythagorean Theorem.
TL;DR
To find the distance between two points, you use the Pythagorean Theorem: take the square root of the horizontal difference squared plus the vertical difference squared. In ActionScript, we store the differences in xDiff and yDiff and return Math.sqrt(xDiff * xDiff + yDiff * yDiff) from a small helper function.
- The x and y values of both points are all you need, since the distance comes from their net horizontal and vertical differences.
- Storing xA - xB and yA - yB in two variables first keeps the final Math.sqrt expression simple.
- Using Math.pow to square values also works, but simple multiplication was around 30 to 40 percent faster in informal tests.
- Distance checks show up in collision detection, games, and the Random Movement animation, which uses distance to decide when to change direction.
There will be times when you need to know how far apart two items are. While it is common in games or situations involving collision detection, you will run into needing to know the distance more often than you think. For example, the Random Movement animation is a case where the distance between two points is used to gauge when to change direction:

In this short tutorial, you will learn how to use the Pythagorean Theorem to calculate the distance when you know the x and y coordinates of two points.
A full explanation and more about the Pythagorean Theorem can be found on Wikipedia, but in a nutshell, the equation looks as follows:

Your distance is the square root of the net horizontal distance squared and your net vertical distance squared. Continuing the example we started off with, the xa, xb, ya, and yb values can be visualized as follows:

The code for converting our equation into the ActionScript that Flash understands is as follows:
- private function GetDistanceSquare(xA:Number, yA:Number, xB:Number, yB:Number):Number
- {
- var xDiff:Number = xA - xB;
- var yDiff:Number = yA - yB;
- return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
- }
Two simplify our expression, I create two variables called xDiff and yDiff that store the difference in the x and y positions between the two points:
- var xDiff:Number = xA - xB;
- var yDiff:Number = yA - yB;
Once I have the difference, the rest of the math is very straightforward:
- Math.sqrt(xDiff * xDiff + yDiff * yDiff);
Below, you will find a larger example that demonstrates finding the distance between two points at (0, 100) and (0, 100):
- public class MainDocument extends MovieClip
- {
- public function MainDocument()
- {
- // constructor code
- var distance:Number = GetDistance(0,0,100,100);
- trace(distance);
- }
- private function GetDistance(xA:Number, yA:Number, xB:Number, yB:Number):Number
- {
- var xDiff:Number = xA - xB;
- var yDiff:Number = yA - yB;
- return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
- }
- }
That's all there is to finding the distance between two points. If there is anything you take away from this tutorial, just ask yourself, wasn't Pythagoras one smart dude?
Note
For calculating squares, there is the Math.pow function that
could be used instead of multiplying the same values twice.
The original version of this tutorial used that approach
actually:
- private function GetDistance(xA:Number, yA:Number, xB:Number, yB:Number):Number
- {
- return Math.sqrt(Math.pow(xA - xB,2) + Math.pow(yA - yB,2));
- }
The reason this approach was not the one presented above is due to performance. Using the simpler multiplying approach, my informal tests showed a gain of around 30 - 40%. While it may not be accurate using the approach I used to measure this, at least it gives you a ballpark. Thanks to @_robfox who pointed this out to me via Twitter.
We use the Pythagorean Theorem. The distance is the square root of the net horizontal distance squared plus the net vertical distance squared. All we need are the x and y coordinates of both points. Take the difference between the x values and the difference between the y values. Square each difference, add them together, and take the square root of the result. Wasn’t Pythagoras one smart dude? In our ActionScript helper function, we store xA - xB in a variable called xDiff and yA - yB in a variable called yDiff. That keeps the rest of the math simple. The function then returns Math.sqrt(xDiff * xDiff + yDiff * yDiff). Math.pow works for calculating squares, and the original version of the code actually used it. Simply multiplying each value by itself was faster, though, with informal tests showing a gain of around 30 to 40 percent. That measurement may not be precise, but it gives us a ballpark. It’s common in games and situations involving collision detection, but we’ll run into it more often than we might think. For example, the Random Movement animation uses the distance between two points to gauge when to change direction. Before you go, here are a few things you can do: Keep reading Read the next article in the series, What is Flex? Why Use It?, or check out some related content: Ask a question Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.Frequently Asked Questions
How Do We Find the Distance Between Two Points?
What’s the Formula for the Distance Between Two Points?
How Do We Calculate the Distance in Code?
Why Multiply Instead of Using Math.pow?
Where Is Finding the Distance Between Two Points Useful?
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇

