Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@athap
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athap/dcc4ec709bf4d0ce2598 to your computer and use it in GitHub Desktop.
Save athap/dcc4ec709bf4d0ce2598 to your computer and use it in GitHub Desktop.
Node - Represent node in the tree
// 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