Last active
September 29, 2015 02:08
-
-
Save sunmockyang/5b74778d892dea3d20bb to your computer and use it in GitHub Desktop.
Simple rectangular bounds
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
| function Bounds(top, left, width, height){ | |
| this.top = (top) ? top : 0; | |
| this.left = (left) ? left : 0; | |
| this.width = (width) ? width : 0; | |
| this.height = (height) ? height : 0; | |
| this.right = this.left + this.width; | |
| this.bottom = this.top + this.height; | |
| }; | |
| Bounds.prototype.move = function(left, top) { | |
| this.top = top; | |
| this.left = left; | |
| this.right = this.left + this.width; | |
| this.bottom = this.top + this.height; | |
| }; | |
| Bounds.prototype.setSize = function(width, height) { | |
| this.width = width; | |
| this.height = height; | |
| this.right = this.left + this.width; | |
| this.bottom = this.top + this.height; | |
| }; | |
| Bounds.prototype.getCenter = function() { | |
| return { | |
| x: this.left + this.width/2, | |
| y: this.top + this.height/2 | |
| } | |
| }; | |
| Bounds.prototype.inBounds = function(x, y) { | |
| return (x > this.left && | |
| x < this.left + this.width && | |
| y > this.top && | |
| y < this.top + this.height); | |
| }; | |
| // Give a point in global space, return point in bound space | |
| Bounds.prototype.relativePoint = function(x, y) { | |
| return { | |
| x: x - this.left, | |
| y: y - this.top | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment