A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: [Flash8]Point object. Big timesaver.

  1. #1
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506

    [Flash8]Point object. Big timesaver.

    Now we don't have to write these functions ourselves, they're built in!

    Point(x:Number, y:Number)
    Creates a new point.

    add(v:Point) : Point
    Adds the coordinates of another point to the coordinates of this point to create a new point.

    clone() : Point
    Creates a copy of this Point object.

    distance(pt1:Point, pt2:Point) : Number
    Returns the distance between pt1 and pt2.

    equals(toCompare:Object) : Boolean
    Determines whether two points are equal.

    interpolate(pt1:Point, pt2:Point, f:Number) : Point
    Determines a point between two specified points.

    normalize(length:Number) : Void
    Scales the line segment between (0,0) and the current point to a set length.

    offset(dx:Number, dy:Number) : Void
    Offsets the Point object by the specified amount.

    polar(len:Number, angle:Number) : Point
    Converts a pair of polar coordinates to a cartesian point coordinate.

    subtract(v:Point) : Point
    Subtracts the coordinates of another point from the coordinates of this point to create a new point.

    toString() : String
    Returns a string that contains the values of the x and y coordinates.


    I'm wondering if they're any faster?

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Tested this
    Code:
    import flash.geom.Point;
    doit = function () {
    	var point_1:Point = new Point (-5, 0);
    	var point_2:Point = new Point (5, 0);
    	var t1:Number = getTimer ();
    	for (var i:Number = 0; i < 1000; i++) {
    		var distanceBetween:Number = Point.distance (point_1, point_2);
    	}
    	trace (getTimer () - t1);
    	var t1:Number = getTimer ();
    	for (var i:Number = 0; i < 1000; i++) {
    		var distX = point_1.x - point_2.x;
    		var distY = point_1.y - point_2.y;
    		var dist = Math.sqrt (distX * distX + distY * distY);
    	}
    	trace (getTimer () - t1);
    };
    intervalId = setInterval(this, "doit", 1000);
    Using distance method is over 10 times slower then using Math.sqrt. In 1000 loop I get results of 110 and 10 ms.

  3. #3
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    That's very interesting, I think I'm going to play with the rest of the flash.geom classes and see if they behave the same way.... Matrix is useless for 3d if you can do it quicker old style...

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Test2:

    var interpolatedPoint:Point = Point.interpolate (point_1, point_2, ip);

    vs

    var distX:Number = point_2.x - point_1.x;
    var distY:Number = point_2.y - point_1.y;
    var interpolatedPoint:Point = new Point (point_1.x + ip * distX, point_1.y + ip * distY);

    The old way is still faster, but not so much anymore. 90-100 ms vs 70-80 ms.


    Test3:

    if (point_1.equals (point_2)) { <<< 15-18 ms
    vs
    if (point_1.x == point_2.x && point_1.y == point_2.y) { <<< 2-3 ms


    Test4:

    var point_2:Point = point_1.clone (); <<< 70-80 ms
    vs
    var point_2:Point = (point_1.x, point1._y); <<< 4-5 ms

    Test5:
    var mylength:Number = point_1.length; <<< 12 ms
    var mylength:Number = Math.sqrt (point_1.x * point_1.x + point_1.y * point_1.y); <<< 8 ms
    Last edited by tonypa; 09-16-2005 at 07:30 AM.

  5. #5
    Razor
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    721
    Quote Originally Posted by tonypa
    Test2:

    var interpolatedPoint:Point = Point.interpolate (point_1, point_2, ip);

    vs

    var distX:Number = point_2.x - point_1.x;
    var distY:Number = point_2.y - point_1.y;
    var interpolatedPoint:Point = new Point (point_1.x + ip * distX, point_1.y + ip * distY);

    The old way is still faster, but not so much anymore. 90-100 ms vs 70-80 ms.


    Test3:

    if (point_1.equals (point_2)) { <<< 15-18 ms
    vs
    if (point_1.x == point_2.x && point_1.y == point_2.y) { <<< 2-3 ms


    Test4:

    var point_2:Point = point_1.clone (); <<<70-80 ms
    vs
    var point_2:Point = (point_1.x, point1._y); <<<4-5 ms
    Which to me makes no sense because u think that macromedia would a) found a way to make it faster, or b) done that exact same way.

  6. #6
    sophisticated
    Join Date
    Sep 2004
    Posts
    288
    Quote Originally Posted by tonypa
    Test2:

    var interpolatedPoint:Point = Point.interpolate (point_1, point_2, ip);

    vs

    var distX:Number = point_2.x - point_1.x;
    var distY:Number = point_2.y - point_1.y;
    var interpolatedPoint:Point = new Point (point_1.x + ip * distX, point_1.y + ip * distY);

    The old way is still faster, but not so much anymore. 90-100 ms vs 70-80 ms.


    Test3:

    if (point_1.equals (point_2)) { <<< 15-18 ms
    vs
    if (point_1.x == point_2.x && point_1.y == point_2.y) { <<< 2-3 ms


    Test4:

    var point_2:Point = point_1.clone (); <<< 70-80 ms
    vs
    var point_2:Point = (point_1.x, point1._y); <<< 4-5 ms

    Test5:
    var mylength:Number = point_1.length; <<< 12 ms
    var mylength:Number = Math.sqrt (point_1.x * point_1.x + point_1.y * point_1.y); <<< 8 ms
    do I read it right: old methods are faster than new ones? what's the point then?

  7. #7
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Quote Originally Posted by chuckury
    Which to me makes no sense because u think that macromedia would a) found a way to make it faster, or b) done that exact same way.
    Yes, but they dont implement for example
    point_1.x == point_2.x && point_1.y == point_2.y
    straight in one line. They have constructed some function, which inherits from some class and maybe checks if points belowng to point class and creates errors when needed. All that takes time of course.

    You could write your own Math.cos function too which could be much faster then built-in function. But would you really want to do that in every game? Point.distance (point_1, point_2) is very nice, short and useful I think

  8. #8
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Quote Originally Posted by MechaPiano
    do I read it right: old methods are faster than new ones? what's the point then?
    Try this
    Code:
    doit = function () {
    	var point_1:Point = new Point (-100, -100);
    	var point_1:Number = 200;
    	var t1:Number = getTimer ();
    	for (var i:Number = 0; i < 1000; i++) {
    		var mylength:Number = point_1.length;
    	}
    	trace (getTimer () - t1);
    	var t1:Number = getTimer ();
    	for (var i:Number = 0; i < 1000; i++) {
    		var mylength:Number = Math.sqrt (point_1.x * point_1.x + point_1.y * point_1.y);
    	}
    	trace (getTimer () - t1);
    };
    This code does not generate error because point_1 is declared twice, first as point, but then it is changed to number. Of course both lenght calculations fail now, but this time using point_1.length is much faster then using Math.sqrt (15 ms vs 80 ms).

  9. #9
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Quote Originally Posted by tonypa
    Test4:

    var point_2:Point = point_1.clone (); <<< 70-80 ms
    vs
    var point_2:Point = (point_1.x, point1._y); <<< 4-5 ms
    I just want to double check this one - I haven't had time to download the trial and play around yet. Is the "(x, y)" notation shortcut for making a Point? Or should that have been "new Point(x, y)"?

    These are of great interest to me, since I use my own rolled vector classes A LOT, so knowing the status of these is helping me very much ( to upgrade or not to upgrade... ).

    Thanks Tony, good stuff.
    jonmack
    flash racer blog - advanced arcade racer development blog

  10. #10
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    I've been reading up about this, and it seems that most of the flash.geom classes are written in actionscript, not native to the player code.

    There this as well:
    http://script.com.ua/dev/materials.php?id=11
    which is a spool of the player init actionscript, apparently it contains all of the non-native functions, though I've found the distance function, It looks like it's then calling the subtract function. Makes no sense.

  11. #11
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Quote Originally Posted by jonmack
    I just want to double check this one - I haven't had time to download the trial and play around yet. Is the "(x, y)" notation shortcut for making a Point? Or should that have been "new Point(x, y)"?
    umm... I dont AS2 too well yet. I think by using ":Point" you are declaring new point already so you can just give x and y to it without the need to say "new Point" again:

    //creates new point
    var my_point:Point = (x, y);

    However, if you dont use strict typing, you would have to say "New Point":

    //creates new point
    my_Point=new Point(x,y);

    //does not create new point, its only normal object
    my_Point={x,y};

    Or something like this
    Im sorry, I dont know correct syntax too well.

  12. #12
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Quote Originally Posted by lesli_felix
    ... though I've found the distance function, It looks like it's then calling the subtract function. Makes no sense.
    yes, it gives same result:

    var resultPoint:Point = point_1.subtract(point_2);
    trace(resultPoint.length);
    var distanceBetween:Number = Point.distance(point_1, point_2);
    trace(distanceBetween);

  13. #13
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    Oh I get it now...its the point.length property that returns the distance.

    So when you call distance(), it calls this:

    distance = function (pt1, pt2) {
    return (pt1.subtract(pt2).length);
    };

    Which then calls this:

    subtract = function (v) {
    return (new flash.geom.Point(this.x - v.x, this.y - v.y));
    };

    and this:

    addProperty("length", function () {
    return (Math.sqrt(this.x * this.x + this.y * this.y));
    }, function (newlen) {});

  14. #14
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    No wonder its slower

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center