In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Find distance between two points

Find distance between two points using Point class and using Pythagorean Theorem.
Get Adobe Flash player
by mucaccino 10 Aug 2011
/**
 * Copyright mucaccino ( http://wonderfl.net/user/mucaccino )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ba02
 */

package {
    import flash.geom.Point;
    import flash.display.Sprite;
    import flash.utils.getTimer;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            inittrace(stage);
            // aux vars
            var init:Number, end:Number, aux:Number;
            // points
            var pt1:Point = new Point(0, 0);
            var pt2:Point = new Point(10, 10);
            // find distance usign Point.distance
            var dt1:Number;
            init = getTimer();
            aux=10000000;
            while(aux>0) {
                dt1 = Point.distance(pt1, pt2);
                aux--;
            }
            end = getTimer();            
            trace('// finded distance using point:\n');
            trace('[distance=' + dt1 + ", execution time: " + (end-init) + "ms]");
            // find distance usign the pythagorean theorem 
            var dt2:Number;
            init = getTimer();
            aux=10000000;
            while(aux>0) {
                dt2 = Util.distance(pt1.x, pt1.y ,pt2.x, pt2.y);
                aux--;
            }
            end = getTimer(); 
            trace('// finded distance using pythagorean theorem:\n');
            trace('[distance=' + dt2 + ", execution time: " + (end-init) + "ms]");
        }        
 
    }
}

/////  CLASS UTIL TO ENCAPSULATE FIND METHOD /////

import flash.geom.Point;

class Util {
    public static function distance (x1:int, y1:int, x2:int, y2:int):Number {
       var dx:int = x2 - x1;
       var dy:int = y2 - y1;
       return Math.sqrt(dx*dx + dy*dy)
    }
}


/////  WONDERFL TRACE /////

import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;


function inittrace(s:Stage):void
{
    WTrace.initTrace(s);
}

//global trace function
var trace:Function;

//wtreace class
class WTrace
{
        private static var FONT:String = "Fixedsys";
        private static var SIZE:Number = 12;
        private static var TextFields:Array = [];
        private static var trace_stage:Stage;
        
        public static function initTrace(stg:Stage):void
        {
            trace_stage = stg;
            trace = wtrace;
        }
        
        private static function scrollup():void
        {
            // maximum number of lines: 100
            if (TextFields.length > 100) 
            {
                var removeme:TextField = TextFields.shift();
                trace_stage.removeChild(removeme);
                removeme = null;
            }
            for(var x:Number=0;x<TextFields.length;x++)
            {
                (TextFields[x] as TextField).y -= SIZE*1.2;
            }
        }
    
        public static function wtrace(... args):void
        {
        
            var s:String="";
            var tracefield:TextField;
            
            for (var i:int;i < args.length;i++)
            {
                // imitating flash:
                // putting a space between the parameters
                if (i != 0) s+=" ";
                s+=args[i].toString();
            }
            

            tracefield= new TextField();
            tracefield.autoSize = "left";
            tracefield.text = s;
            tracefield.y = trace_stage.stageHeight - 20;

            var tf:TextFormat = new TextFormat(FONT, SIZE);
            tracefield.setTextFormat(tf);
            trace_stage.addChild(tracefield);
            scrollup();                      
            TextFields.push(tracefield);
            
        }
}