Created
January 9, 2012 15:26
-
-
Save huckfinnaafb/1583408 to your computer and use it in GitHub Desktop.
This file contains 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
function Graph(w, l, h) { | |
this.w = w; | |
this.l = l; | |
this.h = h || 1; | |
this.nodes = []; | |
} | |
Graph.prototype.toVec2 = function (i) { | |
return new Vec2(i % this.w, Math.floor(i / this.w), 0 /* ??? */ ); | |
}; | |
Graph.prototype.toIndice = function (vec2) { | |
return ((vec2.z * this.getWidth() * this.getHeight()) + (vec2.y * this.getWidth()) + vec2.x); | |
}; | |
Graph.prototype.isVacant = function (vec2) { | |
return (typeof this.getNodes()[this.toIndice(vec2)] === 'undefined'); | |
}; | |
Graph.prototype.inBounds = function (vec2) { | |
return ( | |
(vec2.x >= 0) && | |
(vec2.y >= 0) && | |
(vec2.z >= 0) && | |
(vec2.x < this.getWidth()) && | |
(vec2.y < this.getLength()) && | |
(vec2.z < this.getHeight()) | |
); | |
}; | |
Graph.prototype.isOpen = function (vec2) { | |
return (this.isVacant(vec2) && this.inBounds(vec2)); | |
}; | |
Graph.prototype.insert = function (node) { | |
return this.setVertex(node.vec2, node); | |
}; | |
Graph.prototype.remove = function (vec2) { | |
return this.setVertex(vec2, undefined); | |
}; | |
Graph.prototype.replace = function (node, vec2) { | |
return this.setVertex(vec2, node); | |
}; | |
Graph.prototype.move = function (node, vec2) { | |
if (this.isOpen(vec2)) { | |
this.remove(node.vec2); | |
node.vec2.place(vec2); | |
this.insert(node); | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
Graph.prototype.getProperty = function (prop) { | |
if (this.hasOwnProperty(prop)) { | |
return this[prop]; | |
} | |
}; | |
Graph.prototype.getWidth = function () { | |
return this.getProperty('w'); | |
}; | |
Graph.prototype.getLength = function () { | |
return this.getProperty('l'); | |
}; | |
Graph.prototype.getHeight = function () { | |
return this.getProperty('h'); | |
}; | |
Graph.prototype.getNodes = function () { | |
return this.getProperty('nodes'); | |
}; | |
Graph.prototype.getNode = function (vec2) { | |
return this.getNodes()[this.toIndice(vec2)]; | |
}; | |
Graph.prototype.setProperty = function (prop, val) { | |
if (this.hasOwnProperty(prop)) { | |
this[prop] = val; | |
return val; | |
} | |
}; | |
Graph.prototype.setWidth = function (w) { | |
return this.setProperty('w', w); | |
}; | |
Graph.prototype.setLength = function (l) { | |
return this.setProperty('l', l); | |
}; | |
Graph.prototype.setHeight = function (h) { | |
return this.setProperty('h', h); | |
}; | |
Graph.prototype.setDimensions = function (w, l, h) { | |
this.setWidth(w); | |
this.setLength(l); | |
this.setHeight(h); | |
}; | |
Graph.prototype.setVertex = function (vec2, val) { | |
this.getNodes()[this.toIndice(vec2)] = val; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment