diff options
| -rw-r--r-- | public/assets/javascripts/rectangles/models/surface.js | 43 | ||||
| -rw-r--r-- | test/00-setup.js | 2 | ||||
| -rw-r--r-- | test/07-test-surface.js | 73 |
3 files changed, 67 insertions, 51 deletions
diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index eebd566..a700fd4 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -25,7 +25,7 @@ this.bounds.y.b = rect.y.a } this.bounds.x.b += rect.width() - this.bounds.y.b = Math.max(this.bounds.y.b, rect.height()) + this.bounds.y.b = Math.max(this.bounds.y.b, rect.y.b) this.faces.push(rect) } Surface.prototype.fits_scale = function(v, scale){ @@ -91,12 +91,14 @@ delta.a -= right_edge } - var bottom_edge = position.b + delta.b + bounds.y.a + var bottom_edge = position.b + delta.b - bounds.y.a if (bottom_edge < 0) { delta.b -= bottom_edge } var top_edge = position.b + dimension.b + delta.b - bounds.y.b +// console.log(position.b, 0, delta.b, bounds.y.a, bottom_edge) +// console.log(position.b, dimension.b, delta.b, bounds.y.b, top_edge) if (top_edge > 0) { delta.b -= top_edge } @@ -113,38 +115,37 @@ return old_bounds } - var left_index = this.index_for_x( position.a + dimension.a/2 + delta.a, 0 ) - var center_index = this.index_for_x( position.a + dimension.a/2 + delta.a, left_index ) - var right_index = this.index_for_x( position.a + dimension.a/2 + delta.a, center_index ) - var new_bounds = this.bounds_at_index_with_dimensions(center_index, dimension) - - this.clamp_delta(new_bounds, dimension, position, delta) - - return new_bounds +// var left_index = this.index_for_x( position.a + delta.a, 0 ) +// var center_index = this.index_for_x( position.a + dimension.a/2 + delta.a, left_index ) +// var right_index = this.index_for_x( position.a + dimension.a + delta.a, center_index ) +// var new_bounds = this.bounds_at_index_with_dimensions(center_index, dimension) +// +// this.clamp_delta(new_bounds, dimension, position, delta) +// +// 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() + var bounds = this.faces[left_side].clone() + var intersection - for (var i = left_side+1; i <= right_side; i++) { - if (this.faces[i].y.a > bounds.y.a) { - bounds.y.a = this.faces[i].y.a - } - if (this.faces[i].y.b < bounds.y.b) { - bounds.y.b = this.faces[i].y.b + for (var i = Math.min(left_side+1, right_side); i <= right_side; i++) { + intersection = bounds.y.intersection(this.faces[i].y) + if (intersection.length() < dimension.b) { + bounds = null + break } + bounds.y.assign(intersection) bounds.x.b = this.faces[i].x.b } - - if (bounds.width() > dimension.a || bounds.height() > dimension.b) { + if (! bounds || bounds.width() < dimension.a || bounds.height() < dimension.b || + bounds.y.a > position.b + delta.b || bounds.y.b < position.b + dimension.b + delta.b) { bounds = old_bounds } this.clamp_delta(bounds, dimension, position, delta) return bounds -*/ } Surface.prototype.index_for_x = function(x, min_i){ diff --git a/test/00-setup.js b/test/00-setup.js index 20f9d66..e95c4ea 100644 --- a/test/00-setup.js +++ b/test/00-setup.js @@ -1 +1 @@ -Error.stackTraceLimit = 5 +Error.stackTraceLimit = 1 diff --git a/test/07-test-surface.js b/test/07-test-surface.js index 53a4b73..d060943 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -70,6 +70,46 @@ describe('basic surface', function(){ }) }) +describe('half surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(1, 6), new vec2(2, 6) ) ) + + var position = new vec2(2, 3) + + describe('#clamp_delta()', function(){ + it("does not alter a zero delta", function(){ + var delta = new vec2(0,0) + surface.clamp_delta(surface.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.clamp_delta(surface.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.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( -1, 0 ))) + }) + it("clamps rightward delta", function(){ + var delta = new vec2(10, 0) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 2, 0 ))) + }) + it("clamps upward delta", function(){ + var delta = new vec2(0, 10) + surface.clamp_delta(surface.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.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, -1 ))) + }) + }) +}) + describe('double surface', function(){ var surface = new Surface () surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) @@ -242,7 +282,6 @@ describe('door surface', function(){ 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) @@ -266,93 +305,68 @@ describe('door surface', function(){ 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 ))) + assert.equal(true, delta.eq(new vec2( 1, 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 ))) + assert.equal(true, delta.eq(new vec2( 1, 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 0,10", function(){ var delta = new vec2(0, 10) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 0, 1 ))) }) it("clamps upward delta 1,10", function(){ var delta = new vec2(1, 10) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 1, 1 ))) }) it("clamps upward delta 2,10", function(){ var delta = new vec2(2, 10) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+" <<<__c:w<") - console.log(delta+" <<<<<<<<<") assert.equal(true, delta.eq(new vec2( 2, 1 ))) }) it("clamps upward delta 3,10", function(){ var delta = new vec2(3, 10) var bounds2 = surface.translate(bounds, small, position, delta) - console.log(bounds2+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 3, 1 ))) }) it("clamps upward delta 4,10", function(){ var delta = new vec2(4, 10) var bounds2 = surface.translate(bounds, small, position, delta) - console.log(bounds2+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 4, 1 ))) }) it("clamps upward delta 5,10", function(){ var delta = new vec2(5, 10) var bounds2 = surface.translate(bounds, small, position, delta) - console.log(bounds2+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 5, 1 ))) }) it("clamps upward delta 6,10", function(){ var delta = new vec2(6, 10) var bounds2 = surface.translate(bounds, small, position, delta) - console.log(bounds2+" <<<__c:w<") - console.log(delta+"") - assert.equal(true, delta.eq(new vec2( 6, 1 ))) + assert.equal(true, delta.eq(new vec2( 5, 1 ))) }) -/* it("clamps downward delta", function(){ var delta = new vec2(0, -10) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 0, -1 ))) }) -*/ - }) }) @@ -436,3 +450,4 @@ describe('double door surface', function(){ }) + |
