Please note that ExpatTech no longer operates regular office hours, meeting is by appointment only. Please call +36306031104 for details.

ExpatTech Techblog

Peter Todd 2010.01.29. 13:45

Flash Cookbook - Geometry - Fine tuning Point class Part 1. Distance.

Point class is not a final class therefore we can extend it. To do this is quite useful because the main functions of points are in the package so you don't have to rewrite them. But still there are some extra things what you can override to get better results concerning speed.

Let's see a simple test with the distance method. The distance method is a static type which calculates the distance between to give points. Quite easy and simple to use:

var distance:Number = Point.distance(p1, p2);

This is very useful in several calculations, we won't say anything more because it's that fundamental.

But. Here comes the interesting part. I use the method and my program is really clean, but what do I get for my efforts? Somehow these built in packages run slowly if we run our optimum-searching code. Try it for yourself: on my machine it needed around 117 ms to run a 100.000 times. BUT THIS IS AWESOME!!! - could the little grasshopper say. I don't need to know maths and this function is superfast!

What do WE say? : HA-HA (slow motion).

Since we already have the skeleton of the extended Point class (remember to call super() with default x and y coordinates in the constructor) we override the SLOW distance method.

public static function distance(p1:flash.geom.Point, p2:flash.geom.Point):Number
{
     return Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
}

There you go. And, my little padawan, yes. We came to see where the rabbit hole goes. Actually, we are in the rabbit hole. The result of the same test concerning the speed is: 41 ms. Let me say it again FORTYONE milliseconds.

Let's stop for a minute.

Little Grasshoper: Is this really 3 times faster than the built-in code?

We: Obviously.

We don't say that the errorchecking is the same for our own function and the built-in one. We don't check the type of object passed to the function and blabla, but that is so expensive! You have to be careful as there is no errorchecking but this is still really profitable.

One more thing to know. We have the extended Point class now, let's say the package is org.geometry.

How do you make a difference between this new one and the original class? Here is the trick:

var p1:flash.geom.Point = new flash.geom.Point(10, 10);
var p2:org.geometry.Point = new org.geometry.Point(10, 10);

Note that the classes are prefixed with their packages.

And this is our first step to optimize the Point class.

The code:

package org.geometry
{
    import flash.geom.Point;
   
    public class Point extends flash.geom.Point
    {
        public function Point(x:Number = 0, y:Number = 0)
        {
            super(x, y);
        }
       
        public static function distance(p1:flash.geom.Point, p2:flash.geom.Point):Number
        {
            return Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
        }
    }
}