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.
This is good site and nice point of view.I learnt lots of useful information.
ReplyDeletepython Course in Pune
python Course institute in Chennai
python Training institute in Bangalore
Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision.
ReplyDeleteSelenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
ReplyDeleteIt is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me... devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery
A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.thanks !!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training
First i got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks.
ReplyDeletehardware and networking training in chennai
hardware and networking training in porur
xamarin training in chennai
xamarin training in porur
ios training in chennai
ios training in porur
iot training in chennai
iot training in porur
pendik mitsubishi klima servisi
ReplyDeletemaltepe bosch klima servisi
kadıköy bosch klima servisi
maltepe arçelik klima servisi
kadıköy arçelik klima servisi
kartal samsung klima servisi
ümraniye samsung klima servisi
kartal mitsubishi klima servisi
ümraniye mitsubishi klima servisi