Last active
August 29, 2015 14:06
-
-
Save athap/dcc4ec709bf4d0ce2598 to your computer and use it in GitHub Desktop.
Node - Represent node in the tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Represents the node in the tree. Will be displayed as a small circle in the browser. | |
// x, y --> x, y co-ordinates of the center of circle | |
// r --> radius of the circle | |
// ctx --> context of the canvas | |
// data --> data to be displayed (Only number) | |
var Node = function(x,y,r, ctx, data) { | |
// left child of a node | |
this.leftNode = null; | |
// right child of a node | |
this.rightNode = null; | |
// draw function. Responsible for drawing the node | |
this.draw = function() { | |
ctx.beginPath(); | |
ctx.arc(x, y, r, 0, 2*Math.PI); | |
ctx.stroke(); | |
ctx.closePath(); | |
ctx.strokeText(data, x, y); | |
}; | |
// Simple getters | |
this.getData = function() { return data; }; | |
this.getX = function() { return x; }; | |
this.getY = function() { return y; }; | |
this.getRadius = function() { return r; }; | |
// Returns coordinate for the left child | |
// Go back 3 times radius in x axis and | |
// go down 3 times radius in y axis | |
this.leftCoordinate = function() { | |
return {cx: (x - (3*r)), cy: (y + (3*r))} | |
}; | |
// Same concept as above but for right child | |
this.rightCoordinate = function() { | |
return {cx: (x + (3*r)), cy: (y+(3*r))} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment