Saturday 13 October 2012

TypeScript QuadTree part 1

With Microsoft's release of TypeScript I have decided to implement a QuadTree algorithm based off the pseudo code on wikipedia. This post will be the first in a series of perhaps 3 or 4 posts on my TypeScript QuadTree. This post will detail the creation of the point and quad classes which my QuadTree will be dependent on.

The first step that I took was the creation of the interfaces which TypeScript allows, as you will see these interfaces are very close to that in C#. I have declared a series of methods which must be implemented and the return types and parameters for the methods.

interface IPoint {
    getX(): number;
    getY(): number;
}

interface IQuad {
    getLeft(): number;
    getRight(): number;
    getTop(): number;
    getBottom(): number;
    getWidth(): number;
    getHalfWidth(): number;
    getHeight(): number;
    getHalfHeight(): number;
    getCenter(): IPoint;

    containsPoint(candidate: IPoint): bool;
    intersects(candidate: IQuad): bool;
}

After completing the interfaces it was time to get my hands dirty with the implementations of Point and Quad classes. There is no good reason for using a module to wrap the classes aside from the fact that I could do so.

module Shapes {
    export class Point implements IPoint {
        constructor (private x: number, private y: number) { }

        getX() { return this.x; }
        getY() { return this.y; }
    }

    export class Quad implements IQuad {
        constructor (private point: IPoint, private width: number, private height: number) { }

        getLeft() { return this.point.getX(); }
        getRight() { return this.point.getX() + this.width; }
        getTop() { return this.point.getY(); }
        getBottom() { return this.point.getY() + this.height; }
        getWidth() { return this.width; }
        getHalfWidth() { return this.width / 2; }
        getHeight() { return this.height; }
        getHalfHeight() { return this.height / 2; }
        getCenter() { return new Shapes.Point(this.point.getX() + (this.width / 2), this.point.getY() + (this.height / 2)); }

        containsPoint(candidate: IPoint) {
            var isWithinX = this.getLeft() < candidate.getX() && candidate.getX() < this.getRight();
            var isWithinY = this.getTop() < candidate.getY() && candidate.getY() < this.getBottom();

            return isWithinX && isWithinY;
        }

        intersects(candidate: IQuad) { 
            var isWithinX = this.getLeft() < candidate.getRight() && candidate.getLeft() < this.getRight();
            var isWithinY = this.getTop() < candidate.getBottom() && candidate.getTop() < this.getBottom();

            return isWithinX && isWithinY;
        }
    }
}

Once I had completed the point and quad classes, it was time to run the "compile" the TypeScript into JavaScript and that was easily done with the following command.

tsc quad.ts

The tsc "compiler" took the TypeScript that I had written and output the JavaScript to a file called quad.js. Here is the contents of my newly created quad.js.

var Shapes;
(function (Shapes) {
    var Point = (function () {
        function Point(x, y) {
            this.x = x;
            this.y = y;
        }
        Point.prototype.getX = function () {
            return this.x;
        };
        Point.prototype.getY = function () {
            return this.y;
        };
        return Point;
    })();
    Shapes.Point = Point;    
    var Quad = (function () {
        function Quad(point, width, height) {
            this.point = point;
            this.width = width;
            this.height = height;
        }
        Quad.prototype.getLeft = function () {
            return this.point.getX();
        };
        Quad.prototype.getRight = function () {
            return this.point.getX() + this.width;
        };
        Quad.prototype.getTop = function () {
            return this.point.getY();
        };
        Quad.prototype.getBottom = function () {
            return this.point.getY() + this.height;
        };
        Quad.prototype.getWidth = function () {
            return this.width;
        };
        Quad.prototype.getHalfWidth = function () {
            return this.width / 2;
        };
        Quad.prototype.getHeight = function () {
            return this.height;
        };
        Quad.prototype.getHalfHeight = function () {
            return this.height / 2;
        };
        Quad.prototype.getCenter = function () {
            return new Shapes.Point(this.point.getX() + (this.width / 2), this.point.getY() + (this.height / 2));
        };
        Quad.prototype.containsPoint = function (candidate) {
            var isWithinX = this.getLeft() < candidate.getX() && candidate.getX() < this.getRight();
            var isWithinY = this.getTop() < candidate.getY() && candidate.getY() < this.getBottom();
            return isWithinX && isWithinY;
        };
        Quad.prototype.intersects = function (candidate) {
            var isWithinX = this.getLeft() < candidate.getRight() && candidate.getLeft() < this.getRight();
            var isWithinY = this.getTop() < candidate.getBottom() && candidate.getTop() < this.getBottom();
            return isWithinX && isWithinY;
        };
        return Quad;
    })();
    Shapes.Quad = Quad;    
})(Shapes || (Shapes = {}));

I believe that the TypeScript has much greater readability than the JavaScript generated and like with CoffeeScript didn't have to worry about JavaScript traps and tricks. The JavaScript generated seems to be exactly what I was expecting, although jslint has several critiques of it. My next steps are to get the test harness online and finish the QuadTree implementation.

Thursday 4 October 2012

Lazy<T> .NET 3.5

On a large enterprise applicaiton I work on we are currently unable to upgrade from .NET3.5 to .NET4.0, although there is plenty of situations where we could use the .Lazy<T> class. So in order to get us through until we upgrade to .NET I have implemented a very basic .NET 3.5 version which can easily be replaced with the Microsoft version at a later date.

/// <summary>
/// A pre-.NET4 Lazy<T> implementation
/// </summary>
public class LazyLoader<T>
 where T : class
{
 private readonly object padlock;
 private readonly Func<T> function;

 private bool hasRun;
 private T instance;

 public LazyLoader(Func<T> function)
 {
  this.hasRun = false;
  this.padlock = new object();
  this.function = function;
 }

 public T Value()
 {
  lock (padlock)
  {
   if (!hasRun)
   {
    instance = function.Invoke();

    hasRun = true;
   }
  }

  return instance;
 }
}