From 50ad04ca01da10f87bf907fdf3c185a1d68be506 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 6 Aug 2014 17:41:12 -0400 Subject: surface stuff --- test/07-test-surface.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/07-test-surface.js (limited to 'test/07-test-surface.js') diff --git a/test/07-test-surface.js b/test/07-test-surface.js new file mode 100644 index 0000000..8f69e77 --- /dev/null +++ b/test/07-test-surface.js @@ -0,0 +1,60 @@ +var assert = require("assert") +var vec2 = require("../public/assets/javascripts/rectangles/models/vec2.js") +var Rect = require("../public/assets/javascripts/rectangles/models/rect.js") +var Surface = require("../public/assets/javascripts/rectangles/models/surface.js") + +// [[1 3] [0 4]] front back left right +// [[3 4] [2 4]] front back left right +// [[4 5] [0 4]] front back left right + +describe('basic surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 4), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(4, 6), new vec2(0, 4) ) ) + + var small = new vec2(2, 2) + var large = new vec2(10, 10) + var oblong = new vec2(4, 1) + + describe('#fits()', function(){ + it("fits something small", function(){ + assert.equal(true, !! surface.fits(small)) + }) + it("doesn't fit something large", function(){ + assert.equal(false, !! surface.fits(large)) + }) + it("fits something oblong", function(){ + assert.equal(true, !! surface.fits(oblong)) + }) + }) + describe('#fits_scale()', function(){ + it("fits something large, scaled down", function(){ + assert.equal(true, !! surface.fits_scale(large, 0.1)) + }) + it("doesn't fit something small, scaled up", function(){ + assert.equal(false, !! surface.fits_scale(small, 10)) + }) + it("doesn't fit something oblong, scaled up", function(){ + assert.equal(false, !! surface.fits_scale(oblong, 10)) + }) + }) + + describe('#place()', function(){ + it("fits a small element on the top left", function(){ + var bounds = surface.place(small, new vec2(1,3)) + console.log(bounds) + }) + it("places a small element on the right", function(){ + var bounds = surface.place(small, new vec2(4,6)) + console.log(bounds) + }) + }) + + describe('#clamp()', function(){ + it("", function(){ + }) + }) + +}) + -- cgit v1.2.3-70-g09d2 From 1be685f9fe4a7f3a3e947d45f865fe07c03ddbaf Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 14 Aug 2014 18:53:19 -0400 Subject: describe surface tests --- test/07-test-surface.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'test/07-test-surface.js') diff --git a/test/07-test-surface.js b/test/07-test-surface.js index 8f69e77..b2e4769 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -8,6 +8,36 @@ var Surface = require("../public/assets/javascripts/rectangles/models/surface.js // [[4 5] [0 4]] front back left right describe('basic surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(1, 5), new vec2(0, 4) ) ) + + var small = new vec2(2, 2) + var oblong = new vec2(4, 1) + + // describe placement + // describe dragging up (clamp at top edge) + // describe dragging down (clamp at bottom edge) + // describe dragging left (clamp at left edge) + // describe dragging right (clamp at right edge) +}) + +describe('two-level surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 5), new vec2(0, 6) ) ) + + var small = new vec2(2, 2) + var oblong = new vec2(4, 1) + + // describe placement/centering + // describe dragging up (clamp at top edge) + // describe dragging down (clamp at bottom edge) + // describe dragging left (clamp at left edge) + // describe dragging right (clamp at right edge) + // describe dragging up and right (clamp at top edge, then pop into peninsula space) +}) + +describe('door surface', function(){ var surface = new Surface () surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) surface.add( new Rect( new vec2(3, 4), new vec2(2, 4) ) ) @@ -43,11 +73,11 @@ describe('basic surface', function(){ describe('#place()', function(){ it("fits a small element on the top left", function(){ var bounds = surface.place(small, new vec2(1,3)) - console.log(bounds) +// console.log(bounds) }) it("places a small element on the right", function(){ var bounds = surface.place(small, new vec2(4,6)) - console.log(bounds) +// console.log(bounds) }) }) -- cgit v1.2.3-70-g09d2 From 174077e0a9779f73b1b87253c8c7bae8d03400fb Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 15 Aug 2014 17:04:44 -0400 Subject: clamp stuff --- .../javascripts/rectangles/models/surface.js | 76 ++++++++++++++++++++-- test/07-test-surface.js | 66 ++++++++++++++++++- 2 files changed, 133 insertions(+), 9 deletions(-) (limited to 'test/07-test-surface.js') diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 171376f..8330d41 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -11,17 +11,22 @@ } var Surface = function (face){ - this.width = 0 - this.height = 0 + this.bounds = new Rect (new vec2(0, 0), new vec2(0, 0)) this.faces = [] if (face) { this.add(face) } } Surface.prototype.add = function(rect){ + if (this.faces.length == 0) { + this.bounds.x.a = rect.x.a + this.bounds.x.b = rect.x.a + this.bounds.y.a = rect.y.a + 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.faces.push(rect) - this.width += rect.width() - this.height = Math.max(this.height, rect.height()) } Surface.prototype.fits_scale = function(v, scale){ v = v.clone().mul(scale) @@ -30,7 +35,7 @@ Surface.prototype.fits = function(v){ var faces = this.faces var scratch - if (this.width < v.a || this.height < v.b) { + if (this.bounds.x.b < v.a || this.bounds.y.b < v.b) { return null } for (var i = 0; i < faces.length; i++) { @@ -68,7 +73,65 @@ Surface.prototype.place = function(v, index){ var face, faces = this.faces } - Surface.prototype.bounds = function(index){ + + function center (dimension, position) { + return new vec2( position.a + dimension.a/2, position.b + dimension.b/2 ) + } + function edges (dimension, position) { + return new Rect( position.a, position.a + dimension.a, + position.b, position.b + dimension.b ) + } + + Surface.prototype.clamp_delta = function (bounds, dimension, position, delta) { + var left_edge = position.a + delta.a - bounds.x.a + if (left_edge < 0) { + delta.a -= left_edge + } + + var right_edge = position.a + dimension.a + delta.a - bounds.x.b + if (right_edge > 0) { + delta.a -= right_edge + } + + 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 + if (top_edge > 0) { + delta.b -= top_edge + } + } + + Surface.prototype.fit = function (old_bounds, dimension, position, delta) { + // to reposition one of these fucking things + // assume it's already in a valid position + + this.clamp_delta( this.bounds, dimension, position, delta ) + + // if it's moved and it's within the current bounds, we're done + // it's outside the general bounds, do an initial clamp + // given the center, find the element it wants to lie on + // if it's too short for this element, look to the (left? right? compare dx) for the first element tall enough for it + // move the (right? left?) edge to whatever div that is and move the center + // find the elements containing the left and right edges, and any in between + // + } + + Surface.prototype.index_for_x = function(x){ + if (x < 0 || x > width) { + return -1 + } + for (var i = 0; i < this.faces.length; i++) { + if (this.faces[i].x.contains(x)) { + return i + } + } + return -1 + } + + Surface.prototype.bounds_at_index = function(index){ var bounds = faces[index].clone() var height = faces[index].height() bounds.first = bounds.last = index @@ -104,6 +167,7 @@ } return bounds } + Surface.prototype.clamp = function (bounds, dimension, position, dx, dy) { // we start out with a set of boundaries where we know the object should fall // we then check if we've moved the box off any edge of the bounds diff --git a/test/07-test-surface.js b/test/07-test-surface.js index b2e4769..2a51b7f 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -9,7 +9,67 @@ var Surface = require("../public/assets/javascripts/rectangles/models/surface.js describe('basic surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(1, 5), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(1, 6), new vec2(0, 4) ) ) + + var small = new vec2(2, 2) + var oblong = new vec2(4, 1) + + describe('#clamp_delta()', function(){ + var position = new vec2(2,1) + 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('#place()', function(){ + it("fits a small element on the top left", function(){ + var bounds = surface.place(small, new vec2(1,3)) +// console.log(bounds) + }) + it("places a small element on the right", function(){ + var bounds = surface.place(small, new vec2(4,6)) +// console.log(bounds) + }) + }) + + // describe placement + // describe dragging up (clamp at top edge) + // describe dragging down (clamp at bottom edge) + // describe dragging left (clamp at left edge) + // describe dragging right (clamp at right edge) +}) + +describe('double surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 5), new vec2(0, 4) ) ) var small = new vec2(2, 2) var oblong = new vec2(4, 1) @@ -23,7 +83,7 @@ describe('basic surface', function(){ describe('two-level surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) surface.add( new Rect( new vec2(3, 5), new vec2(0, 6) ) ) var small = new vec2(2, 2) @@ -39,7 +99,7 @@ describe('two-level surface', function(){ describe('door surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) surface.add( new Rect( new vec2(3, 4), new vec2(2, 4) ) ) surface.add( new Rect( new vec2(4, 6), new vec2(0, 4) ) ) -- cgit v1.2.3-70-g09d2 From 5c713e0416daf1b71cd1122ce30eaf53200bbaa8 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 15 Aug 2014 19:18:04 -0400 Subject: triple surface --- test/07-test-surface.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'test/07-test-surface.js') diff --git a/test/07-test-surface.js b/test/07-test-surface.js index b2e4769..48a2836 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -21,6 +21,21 @@ describe('basic surface', function(){ // describe dragging right (clamp at right edge) }) +describe('triple surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 5), new vec2(0, 4) ) ) + + var small = new vec2(2, 2) + var oblong = new vec2(4, 1) + + // describe placement + // describe dragging up (clamp at top edge) + // describe dragging down (clamp at bottom edge) + // describe dragging left (clamp at left edge) + // describe dragging right (clamp at right edge) +}) + describe('two-level surface', function(){ var surface = new Surface () surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) -- cgit v1.2.3-70-g09d2 From a8ed33d8313b093f589a5483cb00c7163b7b8dc3 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 18 Aug 2014 15:01:56 -0400 Subject: surface tests --- .../javascripts/rectangles/models/surface.js | 226 +++++++++-------- .../assets/javascripts/rectangles/models/vec2.js | 4 +- test/01-test-vec2.js | 19 ++ test/07-test-surface.js | 271 ++++++++++++++++----- 4 files changed, 355 insertions(+), 165 deletions(-) (limited to 'test/07-test-surface.js') diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 8330d41..9b82e4e 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -70,16 +70,14 @@ } return null } - Surface.prototype.place = function(v, index){ - var face, faces = this.faces - } - function center (dimension, position) { - return new vec2( position.a + dimension.a/2, position.b + dimension.b/2 ) - } - function edges (dimension, position) { - return new Rect( position.a, position.a + dimension.a, - position.b, position.b + dimension.b ) + function center_for (dimension, position, delta) { + var center = new vec2( position.a + dimension.a/2, position.b + dimension.b/2 ) + if (delta) { + center.a += delta.a + center.b += delta.b + } + return center } Surface.prototype.clamp_delta = function (bounds, dimension, position, delta) { @@ -104,26 +102,38 @@ } } - Surface.prototype.fit = function (old_bounds, dimension, position, delta) { - // to reposition one of these fucking things - // assume it's already in a valid position - + Surface.prototype.translate = function (old_bounds, dimension, position, delta) { this.clamp_delta( this.bounds, dimension, position, delta ) - // if it's moved and it's within the current bounds, we're done - // it's outside the general bounds, do an initial clamp - // given the center, find the element it wants to lie on - // if it's too short for this element, look to the (left? right? compare dx) for the first element tall enough for it - // move the (right? left?) edge to whatever div that is and move the center - // find the elements containing the left and right edges, and any in between - // + 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() + + 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 + } + bounds.x.b = this.faces[i].x.b + } + + if (bounds.width() > dimension.a || bounds.height() > dimension.b) { + bounds = old_bounds + } + + this.clamp_delta(bounds, dimension, position, delta) + return bounds } - Surface.prototype.index_for_x = function(x){ + Surface.prototype.index_for_x = function(x, min_i){ + min_i = min_i || 0 if (x < 0 || x > width) { return -1 } - for (var i = 0; i < this.faces.length; i++) { + for (var i = min_i; i < this.faces.length; i++) { if (this.faces[i].x.contains(x)) { return i } @@ -131,107 +141,113 @@ return -1 } - Surface.prototype.bounds_at_index = function(index){ - var bounds = faces[index].clone() - var height = faces[index].height() - bounds.first = bounds.last = index + Surface.prototype.bounds_at_index_with_dimensions = function(index, dimensions){ + var faces = this.faces + if (index == -1) index = this.faces.length-1 - for (var i = index-1; i >= 0; i--) { + var bounds, intersection + var width = dimensions.a + var height = dimensions.b + + for (var i = index; i >= 0; i--) { var face = faces[i] if (face.y.length() < height) { - continue - } - if (face.y.a > bounds.y.a) { - continue + if (bounds) break + else continue } - if (face.y.b < bounds.y.b) { - continue + if (! bounds) { + bounds = face.clone() + bounds.last = i + } + else if (bounds.width() < width) { + intersection = bounds.y.intersection(face.y) + if (intersection.length() < height) { + break + } + else { + bounds.y.a = intersection.a + bounds.y.b = intersection.b + bounds.x.a = face.x.a + bounds.first = i + continue + } + } + else { + if (face.y.a > bounds.y.a) break + if (face.y.b < bounds.y.b) break } - bounds.x.a = bounds.x.a + bounds.x.a = face.x.a bounds.first = i } - - for (var i = index+1; i < faces.length; i++) { + + for (var i = bounds ? bounds.last+1 : index+1; i < faces.length; i++) { var face = faces[i] if (face.y.length() < height) { - continue + if (bounds) break + else continue } - if (face.y.a > bounds.y.a) { - continue - } - if (face.y.b < bounds.y.b) { - continue - } - bounds.x.b = bounds.x.b + if (! bounds) { + bounds = face.clone() + bounds.first = i + } + else if (bounds.width() < width) { + intersection = bounds.y.intersection(face.y) + if (intersection.length() < height) { + break + } + else { + bounds.y.a = intersection.a + bounds.y.b = intersection.b + bounds.x.b = face.x.b + bounds.last = i + continue + } + } + if (face.y.a > bounds.y.a) break + if (face.y.b < bounds.y.b) break + bounds.x.b = face.x.b bounds.last = i } + + if (! bounds) { + // console.log('no bounds') + return false + } + else if (bounds.width() < width) { + // console.log('too narrow') + return false + } return bounds } - Surface.prototype.clamp = function (bounds, dimension, position, dx, dy) { - // we start out with a set of boundaries where we know the object should fall - // we then check if we've moved the box off any edge of the bounds - // if so, check if the new position is valid with respect to the surface. - // for horizontal movement, this means checking the height and boundaries of - // faces with adjacent indexes - var p = position.clone(), nextIndex, nextFace, newBounds - p.a += dx - p.b += dy - - // left edge - if (p.a < bounds.x.a) { - if (bounds.first == 0) { - p.a = bounds.x.a - } - else { - nextIndex = bounds.first - 1 - nextFace = this.faces[nextIndex] - if (nextFace.y.a <= p.b && p.b + dimension.b <= nextFace.y.b) { - // it appears the div to the left will accomodate this element - } - else { - p.a = bounds.x.a - } - } - } - // right edge - else if (p.a + dimension.a > bounds.x.b) { - if (bounds.last == this.faces.length-1) { - p.a = bounds.x.b - dimension.a - } - else { - nextIndex = bounds.last + 1 - nextFace = this.faces[nextIndex] - if (nextFace.y.a <= p.b && p.b + dimension.b <= nextFace.y.b) { - // it appears the div to the right will accomodate this element - } - else { - p.a = bounds.x.b - dimension.a - } - } + Surface.prototype.bounds_at_index = function(index){ + var faces = this.faces + if (index == -1) index = this.faces.length-1 + + var bounds = faces[index].clone() + var height = faces[index].height() + bounds.first = bounds.last = index + + for (var i = index-1; i >= 0; i--) { + var face = faces[i] + if (face.y.length() < height) break + if (face.y.a > bounds.y.a) break + if (face.y.b < bounds.y.b) break + + bounds.x.a = face.x.a + bounds.first = i } - // for vertical movement, this means checking the height and boundaries of - // elements in the set of bounds' indexes - if (p.b < bounds.y.a) { - for (var i = Math.min(nextIndex, bounds.first), last = Math.max(nextIndex, bounds.last); i < last; i++) { - face = this.faces[i] - // loop over each of the faces in the list - // given a face, figure out if the new top-left is in its bounds - // if so, find which one contains its right edge - // get the lowest height of this set - // then clamp to that lowest height - // additionally, this is the new center-index - // recalculate the bounds -// if (face.x.b < p. -// for (var j = bounds.first+1; j < bounds.last - } - } - // bottom edge, so we can clamp here trivially - else if (p.b + dimension.b > bounds.y.b) { - p.b = bounds.y.b - dimension.b + for (var i = index+1; i < faces.length; i++) { + var face = faces[i] + if (face.y.length() < height) break + if (face.y.a > bounds.y.a) break + if (face.y.b < bounds.y.b) break + + bounds.x.b = face.x.b + bounds.last = i } - // if we're able to move out of bounds in that direction, recalculate the bounds + return bounds } if ('window' in this) { diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 214feb9..a36c614 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -123,12 +123,12 @@ } vec2.prototype.union = function(v){ if (this.intersects(v)) { - return new vec2( min(this.a,v.a), max(this.b, v.b) ) + return new vec2( Math.min(this.a,v.a), Math.max(this.b, v.b) ) } } vec2.prototype.intersection = function(v){ if (this.intersects(v)) { - return new vec2( max(this.a,v.a), min(this.b, v.b) ) + return new vec2( Math.max(this.a,v.a), Math.min(this.b, v.b) ) } } diff --git a/test/01-test-vec2.js b/test/01-test-vec2.js index 6104f92..429c537 100644 --- a/test/01-test-vec2.js +++ b/test/01-test-vec2.js @@ -37,6 +37,25 @@ describe('vec2', function(){ }) }) + describe('#intersection()', function(){ + it('intersects from left', function(){ + var inter = vec.intersection( new vec2( 5, 15 ) ) + assert.equal(true, inter.eq( new vec2(5, 10) )) + }) + it('intersects from right', function(){ + var inter = vec.intersection( new vec2( -5, 5 ) ) + assert.equal(true, inter.eq( new vec2(0, 5) )) + }) + it('intersects inner', function(){ + var inter = vec.intersection( new vec2( 2, 5 ) ) + assert.equal(true, inter.eq( new vec2(2, 5) )) + }) + it('intersects outer', function(){ + var inter = vec.intersection( new vec2( -5, 15 ) ) + assert.equal(true, inter.eq( new vec2(0, 10) )) + }) + }) + describe('#contains()', function(){ it('contains itself', function(){ assert.equal(true, vec.contains( 0 )); diff --git a/test/07-test-surface.js b/test/07-test-surface.js index 2a51b7f..fa05d43 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -7,15 +7,19 @@ var Surface = require("../public/assets/javascripts/rectangles/models/surface.js // [[3 4] [2 4]] front back left right // [[4 5] [0 4]] front back left right +var small = new vec2(2, 2) +var wide = new vec2(5, 1) +var tall = new vec2(1, 5) +var large = new vec2(7, 7) +var position = new vec2(2, 1) + describe('basic surface', function(){ var surface = new Surface () surface.add( new Rect( new vec2(1, 6), new vec2(0, 4) ) ) - var small = new vec2(2, 2) - var oblong = new vec2(4, 1) + var position = new vec2(2, 1) describe('#clamp_delta()', function(){ - var position = new vec2(2,1) it("does not alter a zero delta", function(){ var delta = new vec2(0,0) surface.clamp_delta(surface.bounds, small, position, delta) @@ -48,64 +52,152 @@ describe('basic surface', function(){ }) }) - describe('#place()', function(){ - it("fits a small element on the top left", function(){ - var bounds = surface.place(small, new vec2(1,3)) -// console.log(bounds) + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) }) - it("places a small element on the right", function(){ - var bounds = surface.place(small, new vec2(4,6)) -// console.log(bounds) + it("returns false for large image", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, large) + assert.equal(false, bounds) }) }) - - // describe placement - // describe dragging up (clamp at top edge) - // describe dragging down (clamp at bottom edge) - // describe dragging left (clamp at left edge) - // describe dragging right (clamp at right edge) }) describe('double surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) - surface.add( new Rect( new vec2(3, 5), new vec2(0, 4) ) ) - - var small = new vec2(2, 2) - var oblong = new vec2(4, 1) + surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 6), new vec2(0, 4) ) ) - // describe placement - // describe dragging up (clamp at top edge) - // describe dragging down (clamp at bottom edge) - // describe dragging left (clamp at left edge) - // describe dragging right (clamp at right edge) + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("returns false for large image", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, large) + assert.equal(false, bounds) + }) + }) }) describe('two-level surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) - surface.add( new Rect( new vec2(3, 5), new vec2(0, 6) ) ) - - var small = new vec2(2, 2) - var oblong = new vec2(4, 1) - - // describe placement/centering - // describe dragging up (clamp at top edge) - // describe dragging down (clamp at bottom edge) - // describe dragging left (clamp at left edge) - // describe dragging right (clamp at right edge) - // describe dragging up and right (clamp at top edge, then pop into peninsula space) + surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 6), new vec2(0, 6) ) ) + + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(3,0, 6,6))) + }) + it("generates proper bounds with wide", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with tall", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, tall) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(3,0, 6,6))) + }) + it("generates the same bounds with tall from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, tall) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(3,0, 6,6))) + }) + it("returns false for large image", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, large) + assert.equal(false, bounds) + }) + + }) + }) describe('door surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) - surface.add( new Rect( new vec2(3, 4), new vec2(2, 4) ) ) - surface.add( new Rect( new vec2(4, 6), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(1, 4), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(4, 5), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(5, 8), new vec2(0, 4) ) ) - var small = new vec2(2, 2) - var large = new vec2(10, 10) - var oblong = new vec2(4, 1) + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 4,4))) + }) + it("generates proper bounds from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(5,0, 8,4))) + }) + + it("generates proper bounds for wide from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + it("generates proper bounds for wide from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + it("generates proper bounds for wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + + it("returns false for tall", function(){ + assert.equal(false, surface.bounds_at_index_with_dimensions(0, tall)) + assert.equal(false, surface.bounds_at_index_with_dimensions(1, tall)) + assert.equal(false, surface.bounds_at_index_with_dimensions(2, tall)) + }) + it("returns false for large image", function(){ + assert.equal(false, surface.bounds_at_index_with_dimensions(0, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(1, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(2, large)) + }) + }) describe('#fits()', function(){ it("fits something small", function(){ @@ -114,8 +206,8 @@ describe('door surface', function(){ it("doesn't fit something large", function(){ assert.equal(false, !! surface.fits(large)) }) - it("fits something oblong", function(){ - assert.equal(true, !! surface.fits(oblong)) + it("fits something wide", function(){ + assert.equal(true, !! surface.fits(wide)) }) }) describe('#fits_scale()', function(){ @@ -125,24 +217,87 @@ describe('door surface', function(){ it("doesn't fit something small, scaled up", function(){ assert.equal(false, !! surface.fits_scale(small, 10)) }) - it("doesn't fit something oblong, scaled up", function(){ - assert.equal(false, !! surface.fits_scale(oblong, 10)) + it("doesn't fit something wide, scaled up", function(){ + assert.equal(false, !! surface.fits_scale(wide, 10)) }) }) - describe('#place()', function(){ - it("fits a small element on the top left", function(){ - var bounds = surface.place(small, new vec2(1,3)) -// console.log(bounds) +}) + +describe('double door surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(1, 4), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(4, 5), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(5, 8), new vec2(0, 6) ) ) + surface.add( new Rect( new vec2(8, 10), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(10, 14), new vec2(0, 4) ) ) + + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 4,4))) }) - it("places a small element on the right", function(){ - var bounds = surface.place(small, new vec2(4,6)) -// console.log(bounds) + it("generates proper bounds from left door", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) }) - }) + it("generates proper bounds from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + }) + it("generates proper bounds from right door", function(){ + var bounds = surface.bounds_at_index_with_dimensions(3, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(4, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(10,0, 14,4))) + }) + + it("generates proper bounds for wide from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + it("generates proper bounds for wide from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + it("generates proper bounds for wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(4, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + + it("places tall in middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, tall) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + + var bounds = surface.bounds_at_index_with_dimensions(1, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) - describe('#clamp()', function(){ - it("", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + + var bounds = surface.bounds_at_index_with_dimensions(3, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + + var bounds = surface.bounds_at_index_with_dimensions(4, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + }) + it("returns false for large image", function(){ + assert.equal(false, surface.bounds_at_index_with_dimensions(0, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(1, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(2, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(3, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(4, large)) }) }) -- cgit v1.2.3-70-g09d2 From 559799f707a8c7fbd0d6296dfbce81ac6f9b376a Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 18 Aug 2014 18:42:41 -0400 Subject: translation tests --- .../assets/javascripts/rectangles/models/rect.js | 3 + .../javascripts/rectangles/models/surface.js | 23 +++++++- test/07-test-surface.js | 68 ++++++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) (limited to 'test/07-test-surface.js') 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(){ -- cgit v1.2.3-70-g09d2 From 24870ab6ebd31c18b3533ec96ff04ad16153c844 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 19 Aug 2014 18:19:02 -0400 Subject: bounds tester --- .../assets/javascripts/rectangles/models/rect.js | 1 + .../javascripts/rectangles/models/surface.js | 4 +- public/assets/javascripts/rectangles/util/mouse.js | 2 +- public/assets/test/surface.html | 77 ++++++++++++++++++++++ test/07-test-surface.js | 58 ++++++++++++++-- 5 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 public/assets/test/surface.html (limited to 'test/07-test-surface.js') diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index f23ab9e..67852b4 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -139,6 +139,7 @@ } Rect.prototype.width = function(){ return this.x.length() } Rect.prototype.height = function(){ return this.y.length() } + Rect.prototype.delta = function(){ return new vec2( this.x.magnitude(), this.y.magnitude() ) } Rect.prototype.toString = function(){ var sides = sidesToString(this.sides) var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 3f43ec2..f7fe890 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -113,7 +113,9 @@ return old_bounds } - var center_index = this.index_for_x( position.a + dimension.a/2 + delta.a, 0 ) + 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) diff --git a/public/assets/javascripts/rectangles/util/mouse.js b/public/assets/javascripts/rectangles/util/mouse.js index 06958af..fea3376 100644 --- a/public/assets/javascripts/rectangles/util/mouse.js +++ b/public/assets/javascripts/rectangles/util/mouse.js @@ -8,7 +8,7 @@ // cursor.x.a // cursor.y.a }, - move: function(e, cursor, delta){ + move: function(e, cursor){ // delta.a (x) // delta.b (y) }, diff --git a/public/assets/test/surface.html b/public/assets/test/surface.html new file mode 100644 index 0000000..85e3a7b --- /dev/null +++ b/public/assets/test/surface.html @@ -0,0 +1,77 @@ + + + + + + + + + + diff --git a/test/07-test-surface.js b/test/07-test-surface.js index 8a6d3d0..a9feffa 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -223,10 +223,11 @@ describe('door surface', function(){ }) }) - var position = new vec2(1, 2) + var position = new vec2(1, 1) 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) @@ -277,16 +278,65 @@ describe('door surface', function(){ console.log(new_bounds+"") assert.equal(true, delta.eq(new vec2( 5, 0 ))) }) - it("clamps upward delta", function(){ + */ + it("clamps upward delta 0,10", function(){ var delta = new vec2(0, 10) - surface.translate(bounds, small, position, delta) + 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 ))) + }) +/* it("clamps downward delta", function(){ var delta = new vec2(0, -10) - surface.translate(bounds, small, position, delta) + 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 ))) }) +*/ }) -- cgit v1.2.3-70-g09d2 From 4f1d9a04ee6e598e93de298bd9c5081c68b9db92 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Tue, 19 Aug 2014 23:31:58 -0400 Subject: translation tests passing!! --- .../javascripts/rectangles/models/surface.js | 43 ++++++------- test/00-setup.js | 2 +- test/07-test-surface.js | 73 +++++++++++++--------- 3 files changed, 67 insertions(+), 51 deletions(-) (limited to 'test/07-test-surface.js') 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(){ }) + -- cgit v1.2.3-70-g09d2