summaryrefslogtreecommitdiff
path: root/assets/javascripts/rectangles/models/rect.js
diff options
context:
space:
mode:
Diffstat (limited to 'assets/javascripts/rectangles/models/rect.js')
-rw-r--r--assets/javascripts/rectangles/models/rect.js34
1 files changed, 34 insertions, 0 deletions
diff --git a/assets/javascripts/rectangles/models/rect.js b/assets/javascripts/rectangles/models/rect.js
index a5ae9bf..aca9212 100644
--- a/assets/javascripts/rectangles/models/rect.js
+++ b/assets/javascripts/rectangles/models/rect.js
@@ -53,6 +53,24 @@ window.Rect = (function(){
this.translation.a = this.translation.b = 0
return this
}
+ Rect.prototype.resize = function(translation, sides){
+ var translation = translation || this.translation
+ sides = sides || translation.sides
+
+ if (sides & LEFT) {
+ this.x.a += translation.a
+ }
+ if (sides & RIGHT) {
+ this.x.b += translation.a
+ }
+ if (sides & FRONT) {
+ this.y.a += translation.b
+ }
+ if (sides & BACK) {
+ this.y.b += translation.b
+ }
+ this.translation.a = this.translation.b = 0
+ }
Rect.prototype.contains = function(x,y){
return this.x.contains(x) && this.y.contains(y)
}
@@ -62,6 +80,22 @@ window.Rect = (function(){
Rect.prototype.intersects = function(r){
return this.x.intersects(r.x) && this.y.intersects(r.y)
}
+ Rect.prototype.nearEdge = function (x, y, r) {
+ var edges = 0
+ if (x < this.x.a+r) {
+ edges |= LEFT
+ }
+ else if (x > this.x.b-r) {
+ edges |= RIGHT
+ }
+ if (y < this.y.a+r) {
+ edges |= FRONT
+ }
+ else if (y > this.y.b-r) {
+ edges |= BACK
+ }
+ return edges
+ }
Rect.prototype.width = function(){ return this.x.length() }
Rect.prototype.height = function(){ return this.y.length() }
Rect.prototype.toString = function(){