In this article we will see how to apply the Pythagorean theorem to Actionscript 3.0.
This theorem allows us to calculate the distance in between two points keeping in mind both X coordinates and Y coordinates of the points.
The theorem has more then one practical applications while developing with Flash, but we will discover them as we go forward with the articles that I will publish on this matter.
For now, let's concentrate on how to calculate this distance.
Let's see how...
I create a FLA and save it as 'pitagora.fla', into which I position two MovieClip (of whatever shape) and to which I assign the instance names 'clip_0_mc' and 'clip_1_mc'.
I create a Document Class, an AS file saved as 'Pitagora.as', implemented the following way:
Code:
package
{
import flash.display.MovieClip;
import flash.text.TextField;
public class Pitagora extends MovieClip
{
public function Pitagora()
{
calcolaDistanza();
}
private function calcolaDistanza():void
{
var distanzaX:Number=clip_0_mc.x-clip_1_mc.x;
var distanzaY:Number=clip_0_mc.y-clip_1_mc.y;
var distanza_reale:Number=Math.sqrt(distanzaX*distanzaX+distanzaY*distanzaY);
distanza_txt.text=distanza_reale.toString();
}
}
}
The result:
Lets' analise the code.
The Pythagorean theorem states:
In every right-angled triangle, the surface of the square built on the hypotenuse is equal to the sum of the squares built on the remaining two sides.
For Flash, what is the hypotenuse and what are the remaining two sides?
The first side is formed of the distance in between the X coordinates of the two points (in this case, the X coordinates of the two MovieClip)
The second side is formed of the distance in between the Y coordinates of the two points (in this case, the Y coordinates of the two MovieClip)
The hypotenuse represents the distance that we would like to calculate.
So, starting from the two sides, we simply apply the Pythagorean theorem the following way:
I calculate the length of the first side:
var distanzaX:Number=clip_0_mc.x-clip_1_mc.x;
I calculate the length of the second side:
var distanzaY:Number=clip_0_mc.y-clip_1_mc.y;
NB: Based on the position of the two MovieClip, we could find ourselves with negative values for the sides but the theorem states the square of the length and so whatever squared number (negative or positive) gives a positive number as a result.
I calculate the distance of the hypotenuse (the distance in between the two points) with Math.sqrt (the square of...) of side1 by side1 + side2 by side2
var distanza_reale:Number=Math.sqrt(distanzaX*distanza X+distanzaY*distanzaY);
Finaly I render the distance as a text field (this would be all for this article, we will see later on in other articles what we could do with it)
distanza_txt.text=distanza_reale.toString();
In this other example, I repeat all the calculations inside an interval ENTER_FRAME, drag the balls to see the varying distances.
This is the class I use:
Code:
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
public class Pitagora2 extends MovieClip
{
public function Pitagora2()
{
init();
}
private function init():void
{
stage.frameRate=31;
clip_0_mc.buttonMode=true;
clip_1_mc.buttonMode=true;
this.addEventListener(Event.ENTER_FRAME,calcolaDistanza);
clip_0_mc.addEventListener(MouseEvent.MOUSE_DOWN,muoviClip);
clip_0_mc.addEventListener(MouseEvent.MOUSE_UP,fermaClip);
clip_1_mc.addEventListener(MouseEvent.MOUSE_DOWN,muoviClip);
clip_1_mc.addEventListener(MouseEvent.MOUSE_UP,fermaClip);
}
private function calcolaDistanza(e:Event):void
{
var distanzaX:Number=clip_0_mc.x-clip_1_mc.x;
var distanzaY:Number=clip_0_mc.y-clip_1_mc.y;
var distanza_reale:Number=Math.sqrt(distanzaX*distanzaX+distanzaY*distanzaY);
distanza_txt.text=distanza_reale.toString();
}
private function muoviClip(m:MouseEvent):void
{
m.target.startDrag();
}
private function fermaClip(m:MouseEvent):void
{
m.target.stopDrag();
}
}
}
Stay tuned !