summaryrefslogtreecommitdiff
path: root/assets/javascripts/rectangles/models
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-04-24 16:36:49 -0400
committerJules Laplace <jules@okfoc.us>2014-04-24 16:36:49 -0400
commit0e4bc72fe453797743d63f57db47ef3e0468fc65 (patch)
tree31c8983117d7043ff2af5bc315691900ae80a592 /assets/javascripts/rectangles/models
parent796e1194d13bfa16f41676d81f7d9cdb4b9fc96f (diff)
collide based on a radius
Diffstat (limited to 'assets/javascripts/rectangles/models')
-rw-r--r--assets/javascripts/rectangles/models/rect.js18
-rw-r--r--assets/javascripts/rectangles/models/vec2.js9
2 files changed, 22 insertions, 5 deletions
diff --git a/assets/javascripts/rectangles/models/rect.js b/assets/javascripts/rectangles/models/rect.js
index 86cdae3..51162c0 100644
--- a/assets/javascripts/rectangles/models/rect.js
+++ b/assets/javascripts/rectangles/models/rect.js
@@ -1,6 +1,15 @@
var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8,
FRONT_BACK = FRONT | BACK, LEFT_RIGHT = LEFT | RIGHT
+function sidesToString(sides){
+ var s = ""
+ if (sides & FRONT) s += "front "
+ if (sides & BACK) s += "back "
+ if (sides & LEFT) s += "left "
+ if (sides & RIGHT) s += "right "
+ return s
+}
+
window.rect = (function(){
var rect = function (x0,y0,x1,y1){
if (x0 instanceof vec2) {
@@ -49,17 +58,16 @@ window.rect = (function(){
rect.prototype.contains = function(x,y){
return this.x.contains(x) && this.y.contains(y)
}
+ rect.prototype.containsDisc = function(x,y,r){
+ return this.x.contains(x,r) && this.y.contains(y,r)
+ }
rect.prototype.intersects = function(r){
return this.x.intersects(r.x) && this.y.intersects(r.y)
}
rect.prototype.width = function(){ return this.x.length() }
rect.prototype.height = function(){ return this.y.length() }
rect.prototype.toString = function(){
- var sides = ""
- if (this.sides & FRONT) sides += "front "
- if (this.sides & BACK) sides += "back "
- if (this.sides & LEFT) sides += "left "
- if (this.sides & RIGHT) sides += "right "
+ var sides = sidesToString(this.sides)
var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides
return s
}
diff --git a/assets/javascripts/rectangles/models/vec2.js b/assets/javascripts/rectangles/models/vec2.js
index 3105fec..7307fca 100644
--- a/assets/javascripts/rectangles/models/vec2.js
+++ b/assets/javascripts/rectangles/models/vec2.js
@@ -54,6 +54,15 @@ vec2.normalize = function(){
vec2.prototype.contains = function(n){
return this.a <= n && n <= this.b
}
+vec2.prototype.containsDisc = function(n,r){
+ return this.a <= n-r && n+r <= this.b
+}
+vec2.prototype.clamp = function(n){
+ return clamp(n, this.a, this.b)
+}
+vec2.prototype.clampDisc = function(n,r){
+ return clamp(n, this.a+r, this.b-r)
+}
vec2.prototype.intersects = function(v){
if (this.a < v.a) {
return (v.a < this.b && this.b <= v.b) || (this.a < v.b && v.b <= this.b)