Author Archives: Zevan

Distance Between Point and Line Segment

CLICK HERE TO COPY
Actionscript:

/**

Original function by Pieter Iserbyt:

http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/DistancePoint.java

from Paul Bourke's website:

http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/

*/

function pointToLineDistance(p1:Sprite, p2:Sprite, p3:Sprite):Number {

    var xDelta:Number = p2.x - p1.x;

    var yDelta:Number = p2.y - p1.y;

    if ((xDelta == 0) && (yDelta == 0)) {

        // p1 and p2 cannot be the same point

        p2.x [...]

Posted in Math, graphics algorithms | Tagged , , | 5 Comments

Calculate Slope of a Line

CLICK HERE TO COPY
Actionscript:

// calculate the slope of a line

function calculateSlope(x1:Number, y1:Number, x2:Number, y2:Number):Number {

        // rise over run

    var s:Number = (y1 - y2) / (x1 - x2);

    /*if (x1==x2) {

        // slope is Infinity or -Infinity

    }*/

    return s;

}

 

/**

 Test it out

*/

function draw(x1:Number, y1:Number, [...]

Posted in Math, misc | Tagged , , | Leave a comment

for loop Fun

CLICK HERE TO COPY
Actionscript:

var leng:int = 10;

for (var i:int = 0, j:int = leng;  i <leng; i++, j = leng - i){

    trace(i, j);

}

 

/*outputs

0 10

1 9

2 8

3 7

4 6

5 5

6 4

7 3

8 2

9 1

*/

Looping backwards and forwards.

Posted in Math, misc, one-liners | Tagged , | Leave a comment

QuickBox2D Groups

CLICK HERE TO COPY
Actionscript:

import com.actionsnippet.qbox.*;

 

[SWF (backgroundColor=0xAA0000, width=700, height=600, frameRate=60)]

 

var sim:QuickBox2D = new QuickBox2D(this);

 

sim.createStageWalls();

 

/**

create a dancing pill

*/

// all x and y coords are relative to the center of the group

var partA:QuickObject = sim.addCircle({x:-1, y:0, radius:0.5, restitution:.9});

var partB:QuickObject = sim.addCircle({x:1, y:0, radius:0.5, restitution:.9});

var partC:QuickObject = sim.addBox({x:0, y:0, width:2, height:1});

// all the parts are passed into the objects [...]

Posted in Box2D, QuickBox2D, motion | Tagged , , | 3 Comments