summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--public/assets/javascripts/rectangles/models/rect.js3
-rw-r--r--public/assets/javascripts/rectangles/models/surface.js23
-rw-r--r--test/07-test-surface.js68
3 files changed, 92 insertions, 2 deletions
diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js
index 5952f6a..f23ab9e 100644
--- a/public/assets/javascripts/rectangles/models/rect.js
+++ b/public/assets/javascripts/rectangles/models/rect.js
@@ -95,6 +95,9 @@
Rect.prototype.contains = function(x,y){
return this.x.contains(x) && this.y.contains(y)
}
+ Rect.prototype.contains_point = function(p){
+ return this.x.contains(p.x) && this.y.contains(p.y)
+ }
Rect.prototype.containsDisc = function(x,y,r){
return this.x.containsDisc(x,r) && this.y.containsDisc(y,r)
}
diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js
index 0e0926f..3f43ec2 100644
--- a/public/assets/javascripts/rectangles/models/surface.js
+++ b/public/assets/javascripts/rectangles/models/surface.js
@@ -100,12 +100,28 @@
if (top_edge > 0) {
delta.b -= top_edge
}
+
+ return delta
}
Surface.prototype.translate = function (old_bounds, dimension, position, delta) {
+
this.clamp_delta( this.bounds, dimension, position, delta )
+
+ var new_delta = delta.clone()
+ if (this.clamp_delta(old_bounds, dimension, position, new_delta).eq(delta)) {
+ return old_bounds
+ }
+
+ var center_index = this.index_for_x( position.a + dimension.a/2 + delta.a, 0 )
+ var new_bounds = this.bounds_at_index_with_dimensions(center_index, dimension)
+
+ this.clamp_delta(new_bounds, dimension, position, delta)
- var left_side = this.index_for_x( position.a + delta.a, 0 )
+ return new_bounds
+
+/*
+ var left_side = this.index_for_x( position.a + delta.a, 0 )
var right_side = this.index_for_x( position.a + dimension.a + delta.a, left_side )
var bounds = this.sides[left_side].clone()
@@ -126,11 +142,12 @@
this.clamp_delta(bounds, dimension, position, delta)
return bounds
+*/
}
Surface.prototype.index_for_x = function(x, min_i){
min_i = min_i || 0
- if (x < 0 || x > width) {
+ if (x < 0 || x > this.width) {
return -1
}
for (var i = min_i; i < this.faces.length; i++) {
@@ -162,6 +179,8 @@
else if (bounds.width() < width) {
intersection = bounds.y.intersection(face.y)
if (intersection.length() < height) {
+ // not totally sure if we can clobber the bounds here since this would prevent
+ // us from looking right later
break
}
else {
diff --git a/test/07-test-surface.js b/test/07-test-surface.js
index fa05d43..8a6d3d0 100644
--- a/test/07-test-surface.js
+++ b/test/07-test-surface.js
@@ -210,6 +210,7 @@ describe('door surface', function(){
assert.equal(true, !! surface.fits(wide))
})
})
+
describe('#fits_scale()', function(){
it("fits something large, scaled down", function(){
assert.equal(true, !! surface.fits_scale(large, 0.1))
@@ -222,6 +223,73 @@ describe('door surface', function(){
})
})
+ var position = new vec2(1, 2)
+ var bounds = surface.bounds_at_index_with_dimensions(0, small)
+
+ describe('#translate()', function(){
+ it("does not alter a zero delta", function(){
+ var delta = new vec2(0,0)
+ var new_bounds = surface.translate(bounds, small, position, delta)
+ assert.equal(true, delta.eq(new vec2( 0, 0 )))
+ })
+ it("does not alter a minimal delta", function(){
+ var delta = new vec2(1, 1)
+ surface.translate(bounds, small, position, delta)
+ assert.equal(true, delta.eq(new vec2( 1, 1 )))
+ })
+ it("clamps leftward delta", function(){
+ var delta = new vec2(-10, 0)
+ surface.translate(bounds, small, position, delta)
+ assert.equal(true, delta.eq(new vec2( 0, 0 )))
+ })
+ it("clamps 2px rightward delta", function(){
+ var delta = new vec2(2, 0)
+ surface.translate(bounds, small, position, delta)
+ assert.equal(true, delta.eq(new vec2( 1, 0 )))
+ })
+ it("clamps 3px rightward delta", function(){
+ var delta = new vec2(3, 0)
+ var new_bounds = surface.translate(bounds, small, position, delta)
+ console.log(new_bounds+"")
+ assert.equal(true, delta.eq(new vec2( 3, 0 )))
+ })
+ it("clamps 4px rightward delta", function(){
+ var delta = new vec2(4, 0)
+ var new_bounds = surface.translate(bounds, small, position, delta)
+ console.log(new_bounds+"")
+ assert.equal(true, delta.eq(new vec2( 4, 0 )))
+ })
+ it("clamps 5px rightward delta to new bounds", function(){
+ var delta = new vec2(5, 0)
+ var new_bounds = surface.translate(bounds, small, position, delta)
+ console.log(new_bounds+"")
+ assert.equal(true, delta.eq(new vec2( 5, 0 )))
+ })
+ it("clamps 6px rightward delta", function(){
+ var delta = new vec2(6, 0)
+ var new_bounds = surface.translate(bounds, small, position, delta)
+ console.log(new_bounds+"")
+ assert.equal(true, delta.eq(new vec2( 5, 0 )))
+ })
+ it("clamps 20px rightward delta", function(){
+ var delta = new vec2(7, 0)
+ var new_bounds = surface.translate(bounds, small, position, delta)
+ console.log(new_bounds+"")
+ assert.equal(true, delta.eq(new vec2( 5, 0 )))
+ })
+ it("clamps upward delta", function(){
+ var delta = new vec2(0, 10)
+ surface.translate(bounds, small, position, delta)
+ assert.equal(true, delta.eq(new vec2( 0, 1 )))
+ })
+ it("clamps downward delta", function(){
+ var delta = new vec2(0, -10)
+ surface.translate(bounds, small, position, delta)
+ assert.equal(true, delta.eq(new vec2( 0, -1 )))
+ })
+
+ })
+
})
describe('double door surface', function(){