Overview
To find the distance of any two points on an axis is easy, just subtract them. But what about when you have to find the distance of something not on the axis (a diagonal)? Find the distance between any two points with the Pythagorean theorem. This is an old problem we can look to history and find the Pythagorean theorem and Pythagoras, the Greek we’ve named this after. His theorem states that ‘In any right triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle).’
You may remember it as the formula you memorized in geometry or algebra class ‘a squared plus b squared equals c squared’
a^2 + b^2 = c^2
Okay, but how does that help in flash? You want to find the distance between point a
and point b
. Well c
would be the distance between the two points. We know the formula, solving for c.
c = square root of (a^2 + b^2)
.
c = Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2));
Math.sqrt()
is the square root function, so Math.sqrt(4)=2
.
Math.sqrt(x)
computes and returns the square root of x
.
Math.pow()
is the power function, so Math.pow(4, 2)=16
(4 squared). Math.pow(x, y)
computes and returns x
to the power of y
.
You say I remember using this for triangles and stuff, I just want to know the distance between two points, there’s no triangles.
Well, there actually is a triangle we can draw. Go from your first, along an axis (this makes one side), and the other point, along the other axis (this is another side), and you’ll see that the distance you’re looking for is the third side of the triangle (the hypotenuse).
Example
Here’s a quick interactive flash file to show the idea.
[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2008/02/distance.swf” width=”500″ height=”500″ targetclass=”flashmovie”]
[/kml_flashembed]
Actionscript
[cc lang=”actionscript” tab_size=”2″ lines=”40″]
xmid = Stage.width/2;
ymid = Stage.height/2;
a = _root._ymouse-ymid;
b = _root._xmouse-xmid;
c = Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2));
feedbacka.text = Math.round(a);
feedbackb.text = Math.round(b);
feedbackc.text = Math.round(c);
[/cc]
Download
As usual, here’s the source flash file (flash 8 compatible) to take a look: distance.fla
Thanks for the tutorial.
Just one comment. In your Flash file, the distances “a” and “b” are shown as negatives for half of the plane (when the cursor is above the top vertex of the triangle and to the left of the line marked with “a”.)
Distance is always a positive number (it is a scalar quantity).
This is not the same as the negative x- and y- values shown in my trigonometry ratios interactive.
Yea, I realize that you can’t have a negative distance. The distance I display is just the x and y coordinate from the origin… real distance wouldn’t be negative, should have applied an absolute value to the number before I displayed it. Thanks for pointing that out!
I’ll leave that in the display just to help show which line is which. Maybe it should read x and y coordinates rather than distance a and b..
I need help with some homework here is the question. If a=6 and b=8 is it a right triangle? Why or why not? Could you explain it to me?
If a=6 and b=8 is it a right triangle
good luck with your homework. maybe the interactive flash file will help you create a triangle like the one you’re asking about.