Home » ActionScript 2.0, ActionScript 3.0, Flash CS4, Flash CS5, Flex

Maths and Actionscript 3 code | Using as3 with math

12 March 2011 2 Comments
Share on Facebook

Maths and ActionScript together makes nice Flash effects like particles effect and Flash games that we play daily. Today, lets take a

look at ActionScripting in Maths.

Distance between two points:

Pass the two points as arguments to get the resulting distance between two points.

public function distance(p1:Point, p2:Point):Number {
	var dx:Number = p2.x-p1.x;
	var dy:Number = p2.y-p1.y;

 return Math.sqrt(dx*dx + dy*dy);

Angle/Rotation between two points:

Need to pass the four arguments to get the Angle, (X1,Y1) pointA and (X2,Y2) pointB.

function getAngle (x1:Number, y1:Number, x2:Number, y2:Number):Number
    var dx:Number = x2 - x1;
    var dy:Number = y2 - y1;
    var degree:Number = Math.atan2(dy,dx) * 180/Math.PI
 return degree;


Circular rotation:

Rotate an Object in circular path along a given center.

var radius:Number = 40;
var speed:Number = 0.2;
var angle:Number = 0;
var centerX:Number = 30;
var centerY:Number = 30;
//Call rotate function inside Enterframe or Timer
function rotate( target:MovieClip ):void
	target.x = centerX + Math.cos(angle) * radius;
	target.y = centerY + Math.sin(angle) * radius;
	angle +=  speed;
Credit: http://www.riacodes.com/flash/circular-rotation-with-as3/

Find ODD or EVEN:

Using Modulus operator to find given number is ODD or EVEN

if( inputNumber%2 ==0 )
trace( "Given Number is Even" );
trace( "Given Number is ODD" );

Find intersection point of two lines:

Code to find the intersection point of two lines, pass Point A(X1,Y1), Point B(X2,Y2), Point C(X3,Y3), Point D(X4,Y4).

public static function getLineIntersection (a1:Point,a2:Point,b1:Point,b2:Point):Point{    //calculate directional constants    var 

k1:Number = (a2.y-a1.y) / (a2.x-a1.x);    var k2:Number = (b2.y-b1.y) / (b2.x-b1.x);

    // if the directional constants are equal, the lines are parallel,    // meaning there is no intersection point.    if( k1 == k2 ) 

return null;
    var x:Number,y:Number;    var m1:Number,m2:Number;
    // an infinite directional constant means the line is vertical    if( !isFinite(k1) ) {
    // so the intersection must be at the x coordinate of the line    x = a1.x;    m2 = b1.y - k2 * b1.x;    y = k2 * x + m2;
    // same as above for line 2    } else if ( !isFinite(k2) ) {
    m1 = a1.y - k1 * a1.x;    x = b1.x;    y = k1 * x + m1;
    // if neither of the lines are vertical    } else {
    m1 = a1.y - k1 * a1.x;    m2 = b1.y - k2 * b1.x;    x = (m1-m2) / (k2-k1);    y = k1 * x + m1;
 return new Point(x,y);
Credit: http://www.flexstreamer.com/?tag=as3-intersection-point

Percentage of given number between minValue and maxValue:

To find the percentage of a given number in the minValue and maxValue, Suppose find the percentage of 14 from 6 – 100.


function percentage( X:Number, minValue:Number, maxValue:Number ):Number
	return (X - minValue)/(maxValue - minValue) * 100;


We don't share your email anywhere, grab our rss via feedburner


  • Social Flasher said:


  • admin said:

    Thanks :)

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.