From 725cc3cd23890d5369ae20c27c97465c34168913 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 22 Jul 2014 16:23:57 -0400 Subject: starting math tests --- public/assets/javascripts/rectangles/models/vec2.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'public/assets/javascripts/rectangles/models/vec2.js') diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 9233aec..9c6fd99 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -80,16 +80,22 @@ 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) - } - else if (this.a == v.a) { + if (this.a == v.a) { // || this.b == v.b || this.a == v.b || this.b == v.a) { return true } + else if (this.a < v.a) { + return (v.a < this.b && this.b <= v.b) || (this.a < v.b && v.b <= this.b) + } else if (this.a > v.a) { return (this.a < v.b && v.b <= this.b) || (v.a < this.b && this.b <= v.b) } } + vec2.prototype.adjacent = function(v){ + if (this.a == v.a || this.b == v.b || this.a == v.b || this.b == v.a) { + return true + } + return false + } vec2.prototype.union = function(v){ if (this.intersects(v)) { return new vec2( min(this.a,v.a), max(this.b, v.b) ) @@ -115,8 +121,7 @@ if ('window' in this) { window.vec2 = vec2 } - else if ('module' in this) { + else { module.exports = vec2 } - -})() \ No newline at end of file +})() -- cgit v1.2.3-70-g09d2 From 52d18ddb211a7f4ee814ef23ff09656134810519 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Wed, 23 Jul 2014 01:38:17 -0400 Subject: making things intersect more greedily --- Makefile | 6 ++ .../assets/javascripts/rectangles/models/rect.js | 64 ++---------- .../assets/javascripts/rectangles/models/vec2.js | 39 ++++++- test/01-test-vec2.js | 48 +++++++++ test/02-test-rect.js | 116 +++++++++++++++++++++ test/test-rect.js | 63 ----------- test/test-vec2.js | 48 --------- 7 files changed, 216 insertions(+), 168 deletions(-) create mode 100644 Makefile create mode 100644 test/01-test-vec2.js create mode 100644 test/02-test-rect.js delete mode 100644 test/test-rect.js delete mode 100644 test/test-vec2.js (limited to 'public/assets/javascripts/rectangles/models/vec2.js') diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b7e7836 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ + +test: + ./node_modules/.bin/mocha -R nyan + +.PHONY: test + diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index f91e759..8b6a666 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -95,7 +95,8 @@ return this.x.containsDisc(x,r) && this.y.containsDisc(y,r) } Rect.prototype.intersects = function(r){ - return this.x.intersects(r.x) && this.y.intersects(r.y) + var corner_intersect = (this.x.b === r.x.a && this.y.b === r.y.a) + return this.x.intersects(r.x) && this.y.intersects(r.y) && ! corner_intersect } Rect.prototype.adjacent = function(r){ return this.x.adjacent(r.x) && this.y.adjacent(r.y) @@ -137,70 +138,21 @@ Rect.prototype.split = function(r){ var rz = this var splits = [] - var split_contains = 0 - var x_intervals = [], y_intervals = [] var sides = this.sides - // Split vertically - if (this.x.contains(r.x.a) && r.x.contains(this.x.b)) { - x_intervals.push([ new vec2( this.x.a, r.x.a ), LEFT ]) - x_intervals.push([ new vec2( r.x.a, this.x.b ), RIGHT ]) - split_contains |= RIGHT - } - - else if (r.x.contains(this.x.a) && this.x.contains(r.x.b)) { - x_intervals.push([ new vec2( this.x.a, r.x.b ), LEFT ]) - x_intervals.push([ new vec2( r.x.b, this.x.b ), RIGHT ]) - split_contains |= LEFT - } - - else if (this.x.contains(r.x.a) && this.x.contains(r.x.b)) { - x_intervals.push([ new vec2( this.x.a, r.x.a ), LEFT ]) - x_intervals.push([ new vec2( r.x.a, r.x.b ), 0 ]) - x_intervals.push([ new vec2( r.x.b, this.x.b ), RIGHT ]) - split_contains |= LEFT | RIGHT - } - - else { // if (r.x.contains(this.x.a) && r.x.contains(r.x.b)) { - x_intervals.push([ new vec2( this.x.a, this.x.b ), LEFT | RIGHT ]) - split_contains |= LEFT | RIGHT - } - - // Split horizontally - if (this.y.contains(r.y.a) && r.y.contains(this.y.b)) { - y_intervals.push([ new vec2( this.y.a, r.y.a ), FRONT ]) - y_intervals.push([ new vec2( r.y.a, this.y.b ), BACK ]) - split_contains |= BACK - } - - else if (r.y.contains(this.y.a) && this.y.contains(r.y.b)) { - y_intervals.push([ new vec2( this.y.a, r.y.b ), FRONT ]) - y_intervals.push([ new vec2( r.y.b, this.y.b ), BACK ]) - split_contains |= FRONT - } - - else if (this.y.contains(r.y.a) && this.y.contains(r.y.b)) { - y_intervals.push([ new vec2( this.y.a, r.y.a ), FRONT ]) - y_intervals.push([ new vec2( r.y.a, r.y.b ), 0 ]) - y_intervals.push([ new vec2( r.y.b, this.y.b ), BACK ]) - split_contains |= FRONT | BACK - } - - else { // if (r.y.contains(this.y.a) && this.y.contains(r.y.b)) { - y_intervals.push([ new vec2( this.y.a, this.y.b ), FRONT | BACK ]) - split_contains |= FRONT | BACK - } + var x_intervals = this.x.split( r.x, LEFT, RIGHT ) + var y_intervals = this.y.split( r.y, FRONT, BACK ) x_intervals.forEach(function(x){ y_intervals.forEach(function(y){ var rn = new Rect(x[0], y[0]) rn.id = rz.id rn.sides = ((x[1] | y[1]) & sides) - if (r.intersects(rn)) { - rn.sides = 0 - } +// if (r.intersects(rn)) { +// rn.sides = 0 +// } // if (r.x.b == rn.x.a) { -// rn.sides &= ~LEFT +// rn.sides &= ~LEFT // } // if (rn.x.b == r.x.a) { // rn.sides &= ~RIGHT diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 9c6fd99..4480473 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -80,7 +80,7 @@ return clamp(n, this.a+r, this.b-r) } vec2.prototype.intersects = function(v){ - if (this.a == v.a) { // || this.b == v.b || this.a == v.b || this.b == v.a) { + if (this.a == v.a || this.b == v.b || this.a == v.b || this.b == v.a) { return true } else if (this.a < v.a) { @@ -106,6 +106,43 @@ return new vec2( max(this.a,v.a), min(this.b, v.b) ) } } + + // given two vectors, test how they overlap + // return the set of overlapping segments in the initial vector, labelled with sides + vec2.prototype.split = function(v, left, right){ + var intervals = [] + + if (this.eq(v)) { + intervals.push([ new vec2( this.a, this.b ), left | right ]) + } + + // a---A===b---B (rightways overlap) + else if (this.contains(v.a) && v.contains(this.b)) { + intervals.push([ new vec2( this.a, v.a ), left ]) + intervals.push([ new vec2( v.a, this.b ), 0 ]) + } + + // A---a===B---b (leftways overlap) + else if (v.contains(this.a) && this.contains(v.b)) { + intervals.push([ new vec2( this.a, v.b ), 0 ]) + intervals.push([ new vec2( v.b, this.b ), right ]) + } + + // a---A===B---b (contains v) + else if (this.contains(v.a) && this.contains(v.b)) { + intervals.push([ new vec2( this.a, v.a ), left ]) + intervals.push([ new vec2( v.a, v.b ), 0 ]) + intervals.push([ new vec2( v.b, this.b ), right ]) + } + + // A---a===b---B (contained in v) + else { // if (v.contains(this.a) && v.contains(v.b)) { + intervals.push([ new vec2( this.a, this.b ), 0 ]) + } + + return intervals + } + vec2.prototype.toString = function(){ return "[" + ~~this.a + " " + ~~this.b + "]" } diff --git a/test/01-test-vec2.js b/test/01-test-vec2.js new file mode 100644 index 0000000..054d37b --- /dev/null +++ b/test/01-test-vec2.js @@ -0,0 +1,48 @@ +var assert = require("assert") +var vec2 = require("../public/assets/javascripts/rectangles/models/vec2.js") + +describe('vec2', function(){ + describe('#intersects()', function(){ + var vec = new vec2(0, 10) + + it('intersects itself', function(){ + assert.equal(true, vec.intersects( new vec2(0, 10) )); + }) + it('intersects w/ same startpoint (shorter)', function(){ + assert.equal(true, vec.intersects( new vec2(0, 5) )); + }) + it('intersects w/ same startpoint (longer)', function(){ + assert.equal(true, vec.intersects( new vec2(0, 15) )); + }) + it('intersects w/ same endpoint (shorter)', function(){ + assert.equal(true, vec.intersects( new vec2(5, 10) )); + }) + it('intersects w/ same endpoint (longer)', function(){ + assert.equal(true, vec.intersects( new vec2(-5, 10) )); + }) + it('intersects when contained', function(){ + assert.equal(true, vec.intersects( new vec2(-5, 15) )); + }) + it('does not intersect when before', function(){ + assert.equal(false, vec.intersects( new vec2(-10, -5) )); + }) + it('does not intersect when after', function(){ + assert.equal(false, vec.intersects( new vec2(15, 20) )); + }) + it('contains itself', function(){ + assert.equal(true, vec.contains( 0 )); + assert.equal(true, vec.contains( 5 )); + assert.equal(true, vec.contains( 10 )); + }) + it('does not contain before or after', function(){ + assert.equal(false, vec.contains( -5 )); + assert.equal(false, vec.contains( 15 )); + }) + it('intersects when only startpoint matches', function(){ + assert.equal(true, vec.intersects( new vec2(-5, 0) )); + }) + it('intersects when only endpoint matches', function(){ + assert.equal(true, vec.intersects( new vec2(10, 15) )); + }) + }) +}) diff --git a/test/02-test-rect.js b/test/02-test-rect.js new file mode 100644 index 0000000..39693f5 --- /dev/null +++ b/test/02-test-rect.js @@ -0,0 +1,116 @@ +var assert = require("assert") +var vec = require("../public/assets/javascripts/rectangles/models/vec2.js") +var Rect = require("../public/assets/javascripts/rectangles/models/rect.js") +var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 +var ALL = FRONT | BACK | LEFT | RIGHT + +describe('rect', function(){ + describe('#intersects()', function(){ + var rect = new Rect(0, 0, 10, 10) + + it('intersects itself', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 10, 10) )); + }) + it('intersects more wide', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 5, 10) )); + }) + it('intersects less wide', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 15, 10) )); + }) + it('intersects more tall', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 10, 5) )); + }) + it('intersects less tall', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 10, 15) )); + }) + it('intersects if right-adjacent', function(){ + assert.equal(true, rect.intersects( new Rect(10, 0, 20, 10) )); + }) + it('intersects if bottom-adjacent', function(){ + assert.equal(true, rect.intersects( new Rect(0, 10, 10, 20) )); + }) + it('does not intersect if to the right', function(){ + assert.equal(false, rect.intersects( new Rect(20, 0, 40, 10) )); + }) + it('does not intersect if beneath', function(){ + assert.equal(false, rect.intersects( new Rect(0, 20, 10, 40) )); + }) + /* + it('does not intersect if corners intersect', function(){ + assert.equal(false, rect.intersects( new Rect(10, 10, 20, 20) )); + }) + */ + + }) + + var rect = new Rect( new vec(1,4), new vec(1,4) ) + + var east_in = new Rect( new vec(2,3), new vec(1,4) ) + var east_edge = new Rect( new vec(2,4), new vec(1,4) ) + var east = new Rect( new vec(2,5), new vec(1,4) ) + + var south_in = new Rect( new vec(1,4), new vec(2,3) ) + var south_edge = new Rect( new vec(1,4), new vec(2,4) ) + var south = new Rect( new vec(1,4), new vec(2,5) ) + + var corner = new Rect( new vec(3,5), new vec(3,5) ) + + function sides (s) { + return s.reduce(function(prev, curr){ + return prev | curr.sides + }, 0) + } + + describe('#split(rect, east)', function(){ + var s0 = rect.split(east) + var s1 = east.split(rect) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is front/back/left', function(){ + assert.equal(FRONT | BACK | LEFT, sides(s0)) + }) + it('east is front/back/right', function(){ + assert.equal(FRONT | BACK | RIGHT, sides(s1)) + }) + }) + + describe('#split(rect, east_in)', function(){ + var s0 = rect.split(east_in) + var s1 = east_in.split(rect) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is has all sides', function(){ + assert.equal(ALL, sides(s0)) + }) + it('east_in only has front/back', function(){ + assert.equal(FRONT | BACK, sides(s1)) + }) + }) + + describe('#split(rect, east_edge)', function(){ + var s0 = rect.split(east_edge) + var s1 = east_edge.split(rect) + + console.log("\n") + console.log(rect+"") + console.log(east_edge+"") + console.log(s0.map(function(r){ return r.toString() })) + console.log(s1.map(function(r){ return r.toString() })) + + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is front/back/left', function(){ + assert.equal(FRONT | BACK | LEFT, sides(s0)) + }) + it('east is front/back/right', function(){ + assert.equal(FRONT | BACK | RIGHT, sides(s1)) + }) + }) + +}) +/* +*/ + diff --git a/test/test-rect.js b/test/test-rect.js deleted file mode 100644 index 7dd7ae6..0000000 --- a/test/test-rect.js +++ /dev/null @@ -1,63 +0,0 @@ -var assert = require("assert") -var vec = require("../public/assets/javascripts/rectangles/models/vec2.js") -var Rect = require("../public/assets/javascripts/rectangles/models/rect.js") -var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 -var ALL = FRONT | BACK | LEFT | RIGHT - -describe('rect', function(){ - describe('#intersects()', function(){ - var rect = new Rect(0, 0, 10, 10) - - it('intersects itself', function(){ - assert.equal(true, rect.intersects( new Rect(0, 0, 10, 10) )); - }) - it('intersects more wide', function(){ - assert.equal(true, rect.intersects( new Rect(0, 0, 5, 10) )); - }) - it('intersects less wide', function(){ - assert.equal(true, rect.intersects( new Rect(0, 0, 15, 10) )); - }) - it('intersects more tall', function(){ - assert.equal(true, rect.intersects( new Rect(0, 0, 10, 5) )); - }) - it('intersects less tall', function(){ - assert.equal(true, rect.intersects( new Rect(0, 0, 10, 15) )); - }) - it('intersects if right-adjacent', function(){ - assert.equal(true, rect.intersects( new Rect(10, 0, 20, 10) )); - }) - it('intersects if bottom-adjacent', function(){ - assert.equal(true, rect.intersects( new Rect(0, 10, 10, 20) )); - }) - it('does not intersect if to the right', function(){ - assert.equal(false, rect.intersects( new Rect(20, 0, 40, 10) )); - }) - it('does not intersect if beneath', function(){ - assert.equal(false, rect.intersects( new Rect(0, 20, 10, 40) )); - }) - /* - it('does not intersect if corners intersect', function(){ - assert.equal(false, rect.intersects( new Rect(10, 10, 20, 20) )); - }) - */ - - }) - describe('#split()', function(){ - var rect = new Rect( 0, 0, 10, 10) - var east = new Rect( 5, 0, 15, 10) - var east_in = new Rect( 5, 0, 10, 10) - var east_edge = new Rect(10, 0, 20, 10) - var south = new Rect( 0, 5, 10, 15) - var south_in = new Rect( 0, 5, 10, 10) - var south_edge = new Rect( 0, 10, 10, 15) - var corner = new Rect( 5, 5, 15, 15) - - it('splits east', function(){ - var splits = rect.split(east) - var splits2 = east.split(rect) - console.log("\n") - console.log(rect+"") - console.log(splits.map(function(r){ return r.toString() })) - }) - }) -}) diff --git a/test/test-vec2.js b/test/test-vec2.js deleted file mode 100644 index 054d37b..0000000 --- a/test/test-vec2.js +++ /dev/null @@ -1,48 +0,0 @@ -var assert = require("assert") -var vec2 = require("../public/assets/javascripts/rectangles/models/vec2.js") - -describe('vec2', function(){ - describe('#intersects()', function(){ - var vec = new vec2(0, 10) - - it('intersects itself', function(){ - assert.equal(true, vec.intersects( new vec2(0, 10) )); - }) - it('intersects w/ same startpoint (shorter)', function(){ - assert.equal(true, vec.intersects( new vec2(0, 5) )); - }) - it('intersects w/ same startpoint (longer)', function(){ - assert.equal(true, vec.intersects( new vec2(0, 15) )); - }) - it('intersects w/ same endpoint (shorter)', function(){ - assert.equal(true, vec.intersects( new vec2(5, 10) )); - }) - it('intersects w/ same endpoint (longer)', function(){ - assert.equal(true, vec.intersects( new vec2(-5, 10) )); - }) - it('intersects when contained', function(){ - assert.equal(true, vec.intersects( new vec2(-5, 15) )); - }) - it('does not intersect when before', function(){ - assert.equal(false, vec.intersects( new vec2(-10, -5) )); - }) - it('does not intersect when after', function(){ - assert.equal(false, vec.intersects( new vec2(15, 20) )); - }) - it('contains itself', function(){ - assert.equal(true, vec.contains( 0 )); - assert.equal(true, vec.contains( 5 )); - assert.equal(true, vec.contains( 10 )); - }) - it('does not contain before or after', function(){ - assert.equal(false, vec.contains( -5 )); - assert.equal(false, vec.contains( 15 )); - }) - it('intersects when only startpoint matches', function(){ - assert.equal(true, vec.intersects( new vec2(-5, 0) )); - }) - it('intersects when only endpoint matches', function(){ - assert.equal(true, vec.intersects( new vec2(10, 15) )); - }) - }) -}) -- cgit v1.2.3-70-g09d2 From 8a8b78b3f6a4ce930263099fe14b0fe86d11597f Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Wed, 23 Jul 2014 11:11:08 -0400 Subject: shorter stack traces --- .../assets/javascripts/rectangles/models/rect.js | 26 +++++----- .../assets/javascripts/rectangles/models/vec2.js | 21 ++++++-- test/00-setup.js | 2 + test/02-test-rect.js | 56 ++++++++++++++++++---- 4 files changed, 80 insertions(+), 25 deletions(-) create mode 100644 test/00-setup.js (limited to 'public/assets/javascripts/rectangles/models/vec2.js') diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 8b6a666..f724ecc 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -148,20 +148,22 @@ var rn = new Rect(x[0], y[0]) rn.id = rz.id rn.sides = ((x[1] | y[1]) & sides) -// if (r.intersects(rn)) { -// rn.sides = 0 -// } - // if (r.x.b == rn.x.a) { -// rn.sides &= ~LEFT -// } -// if (rn.x.b == r.x.a) { -// rn.sides &= ~RIGHT + + if (r.x.contains(rn.x.a)) { + rn.sides &= ~ LEFT + } + if (r.x.contains(rn.x.b)) { + rn.sides &= ~ RIGHT + } +// if (r.y.contains(rn.y.a)) { +// rn.sides &= ~ FRONT // } -// if (r.y.b == rn.y.a) { -// rn.sides &= ~FRONT +// if (r.y.contains(rn.y.a)) { +// rn.sides &= ~ BACK // } -// if (rn.y.b == r.y.a) { -// rn.sides &= ~BACK + +// if (r.intersects(rn)) { +// rn.sides = 0 // } rn.focused = rz.focused splits.push(rn) diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 4480473..447c7a3 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -110,7 +110,7 @@ // given two vectors, test how they overlap // return the set of overlapping segments in the initial vector, labelled with sides vec2.prototype.split = function(v, left, right){ - var intervals = [] + var intervals = [], _len if (this.eq(v)) { intervals.push([ new vec2( this.a, this.b ), left | right ]) @@ -119,12 +119,12 @@ // a---A===b---B (rightways overlap) else if (this.contains(v.a) && v.contains(this.b)) { intervals.push([ new vec2( this.a, v.a ), left ]) - intervals.push([ new vec2( v.a, this.b ), 0 ]) + intervals.push([ new vec2( v.a, this.b ), right ]) } // A---a===B---b (leftways overlap) else if (v.contains(this.a) && this.contains(v.b)) { - intervals.push([ new vec2( this.a, v.b ), 0 ]) + intervals.push([ new vec2( this.a, v.b ), left ]) intervals.push([ new vec2( v.b, this.b ), right ]) } @@ -137,7 +137,20 @@ // A---a===b---B (contained in v) else { // if (v.contains(this.a) && v.contains(v.b)) { - intervals.push([ new vec2( this.a, this.b ), 0 ]) + intervals.push([ new vec2( this.a, this.b ), left | right ]) + } + + // cull empty vectors + _len = intervals.length + if (_len > 1) { + if (intervals[0][0].magnitude() == 0) { + intervals[1][1] |= intervals[0][1] + intervals.shift() + } + else if (intervals[_len-1][0].magnitude() == 0) { + intervals[_len-2][1] |= intervals[_len-1][1] + intervals.pop() + } } return intervals diff --git a/test/00-setup.js b/test/00-setup.js new file mode 100644 index 0000000..8d06a13 --- /dev/null +++ b/test/00-setup.js @@ -0,0 +1,2 @@ +Error.stackTraceLimit = 1 + diff --git a/test/02-test-rect.js b/test/02-test-rect.js index 39693f5..0ae59f7 100644 --- a/test/02-test-rect.js +++ b/test/02-test-rect.js @@ -64,6 +64,13 @@ describe('rect', function(){ describe('#split(rect, east)', function(){ var s0 = rect.split(east) var s1 = east.split(rect) + + console.log("\n") + console.log(rect+"") + console.log(east+"") + console.log(s0.map(function(r){ return r.toString() })) + console.log(s1.map(function(r){ return r.toString() })) + it('splits on all 4 sides', function(){ assert.equal(ALL, sides(s0) | sides(s1)) }) @@ -92,13 +99,16 @@ describe('rect', function(){ describe('#split(rect, east_edge)', function(){ var s0 = rect.split(east_edge) var s1 = east_edge.split(rect) - - console.log("\n") - console.log(rect+"") - console.log(east_edge+"") - console.log(s0.map(function(r){ return r.toString() })) - console.log(s1.map(function(r){ return r.toString() })) - + it('has no degenerate vectors', function(){ + s0.forEach(function(r){ + assert.notEqual(0, r.x.magnitude()) + assert.notEqual(0, r.y.magnitude()) + }) + s1.forEach(function(r){ + assert.notEqual(0, r.x.magnitude()) + assert.notEqual(0, r.y.magnitude()) + }) + }) it('splits on all 4 sides', function(){ assert.equal(ALL, sides(s0) | sides(s1)) }) @@ -110,7 +120,35 @@ describe('rect', function(){ }) }) + return + describe('#split(rect, south)', function(){ + var s0 = rect.split(south) + var s1 = south.split(rect) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is front/left/right', function(){ + assert.equal(FRONT | LEFT | RIGHT, sides(s0)) + }) + it('south is back/left/right', function(){ + assert.equal(BACK | LEFT | RIGHT, sides(s1)) + }) + }) + + describe('#split(rect, corner)', function(){ + var s0 = rect.split(corner) + var s1 = corner.split(rect) + + it('splits on all 4 sides', function(){ + // assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is front/left/right', function(){ + // assert.equal(FRONT | LEFT | RIGHT, sides(s0)) + }) + it('corner is back/left/right', function(){ +// assert.equal(BACK| LEFT | RIGHT, sides(s1)) + }) + }) + }) -/* -*/ -- cgit v1.2.3-70-g09d2 From c3d855b3f0b6af000c0d359da6a2b774bcd0a5d5 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Wed, 23 Jul 2014 15:15:21 -0400 Subject: handling coplanar walls correctly --- .../assets/javascripts/rectangles/models/rect.js | 43 ++++++++++---- .../assets/javascripts/rectangles/models/vec2.js | 3 + test/01-test-vec2.js | 7 +++ test/02-test-rect.js | 65 +++++++++++++++++----- 4 files changed, 93 insertions(+), 25 deletions(-) (limited to 'public/assets/javascripts/rectangles/models/vec2.js') diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index f724ecc..c14d62c 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -143,23 +143,46 @@ var x_intervals = this.x.split( r.x, LEFT, RIGHT ) var y_intervals = this.y.split( r.y, FRONT, BACK ) - x_intervals.forEach(function(x){ - y_intervals.forEach(function(y){ + x_intervals.forEach(function(x, i){ + y_intervals.forEach(function(y, i){ var rn = new Rect(x[0], y[0]) rn.id = rz.id rn.sides = ((x[1] | y[1]) & sides) - if (r.x.contains(rn.x.a)) { - rn.sides &= ~ LEFT + if (r.x.contains(rn.x.a) && r.x.contains(rn.x.b)) { + if (rz.y.a == rn.y.a && r.y.containsCenter(rn.y.a)) { // top edges + rn.sides &= ~ FRONT + } + if (rz.y.b == rn.y.b && r.y.containsCenter(rn.y.b)) { // bottom edges + rn.sides &= ~ BACK + } } - if (r.x.contains(rn.x.b)) { - rn.sides &= ~ RIGHT + + if (r.y.contains(rn.y.a) && r.y.contains(rn.y.b)) { + if (rz.x.a == rn.x.a && r.x.containsCenter(rn.x.a)) { // left edges + rn.sides &= ~ LEFT + } + + if (rz.x.b == rn.x.b && r.x.containsCenter(rn.x.b) ) { // right edges + rn.sides &= ~ RIGHT + } } -// if (r.y.contains(rn.y.a)) { -// rn.sides &= ~ FRONT + +// if (rz.x.contains(r.x.a) || rz.x.contains(r.x.b)) { +// if (r.x.contains(rn.x.b)) { +// rn.sides &= ~ LEFT +// } +// if (r.x.contains(rn.x.b)) { +// rn.sides &= ~ RIGHT +// } // } -// if (r.y.contains(rn.y.a)) { -// rn.sides &= ~ BACK +// if (rz.y.contains(r.y.a) || rz.y.contains(r.y.b)) { +// if (r.y.contains(rn.y.a)) { +// rn.sides &= ~ FRONT +// } +// if (r.y.contains(rn.y.b)) { +// rn.sides &= ~ BACK +// } // } // if (r.intersects(rn)) { diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 447c7a3..5c2b519 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -70,6 +70,9 @@ vec2.prototype.contains = function(n){ return this.a <= n && n <= this.b } + vec2.prototype.containsCenter = function(n){ + return this.a < n && n < this.b + } vec2.prototype.containsDisc = function(n,r){ return this.a <= n-r && n+r <= this.b } diff --git a/test/01-test-vec2.js b/test/01-test-vec2.js index 054d37b..b38f052 100644 --- a/test/01-test-vec2.js +++ b/test/01-test-vec2.js @@ -38,6 +38,13 @@ describe('vec2', function(){ assert.equal(false, vec.contains( -5 )); assert.equal(false, vec.contains( 15 )); }) + it('containsCenter itself', function(){ + assert.equal(true, vec.containsCenter( 5 )); + }) + it('does not containsCenter its endpoints', function(){ + assert.equal(false, vec.containsCenter( 0 )); + assert.equal(false, vec.containsCenter( 10 )); + }) it('intersects when only startpoint matches', function(){ assert.equal(true, vec.intersects( new vec2(-5, 0) )); }) diff --git a/test/02-test-rect.js b/test/02-test-rect.js index 0ae59f7..55f1ec9 100644 --- a/test/02-test-rect.js +++ b/test/02-test-rect.js @@ -60,17 +60,15 @@ describe('rect', function(){ return prev | curr.sides }, 0) } + /* + console.log(s0.map(function(r){ return r.toString() })) + console.log(s1.map(function(r){ return r.toString() })) + */ describe('#split(rect, east)', function(){ var s0 = rect.split(east) var s1 = east.split(rect) - console.log("\n") - console.log(rect+"") - console.log(east+"") - console.log(s0.map(function(r){ return r.toString() })) - console.log(s1.map(function(r){ return r.toString() })) - it('splits on all 4 sides', function(){ assert.equal(ALL, sides(s0) | sides(s1)) }) @@ -99,6 +97,7 @@ describe('rect', function(){ describe('#split(rect, east_edge)', function(){ var s0 = rect.split(east_edge) var s1 = east_edge.split(rect) + it('has no degenerate vectors', function(){ s0.forEach(function(r){ assert.notEqual(0, r.x.magnitude()) @@ -113,17 +112,17 @@ describe('rect', function(){ assert.equal(ALL, sides(s0) | sides(s1)) }) it('rect is front/back/left', function(){ - assert.equal(FRONT | BACK | LEFT, sides(s0)) + assert.equal(ALL, sides(s0)) }) it('east is front/back/right', function(){ assert.equal(FRONT | BACK | RIGHT, sides(s1)) }) }) - return describe('#split(rect, south)', function(){ var s0 = rect.split(south) var s1 = south.split(rect) + it('splits on all 4 sides', function(){ assert.equal(ALL, sides(s0) | sides(s1)) }) @@ -135,18 +134,54 @@ describe('rect', function(){ }) }) + describe('#split(rect, south_in)', function(){ + var s0 = rect.split(south_in) + var s1 = south_in.split(rect) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is has all sides', function(){ + assert.equal(ALL, sides(s0)) + }) + it('south_in only has left/right', function(){ + assert.equal(LEFT | RIGHT, sides(s1)) + }) + }) + + describe('#split(rect, south_edge)', function(){ + var s0 = rect.split(south_edge) + var s1 = south_edge.split(rect) + + it('has no degenerate vectors', function(){ + s0.forEach(function(r){ + assert.notEqual(0, r.x.magnitude()) + assert.notEqual(0, r.y.magnitude()) + }) + s1.forEach(function(r){ + assert.notEqual(0, r.x.magnitude()) + assert.notEqual(0, r.y.magnitude()) + }) + }) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect has all sides', function(){ + assert.equal(ALL, sides(s0)) + }) + it('south is back/left/right', function(){ + assert.equal(BACK | LEFT | RIGHT, sides(s1)) + }) + }) + describe('#split(rect, corner)', function(){ var s0 = rect.split(corner) var s1 = corner.split(rect) - it('splits on all 4 sides', function(){ - // assert.equal(ALL, sides(s0) | sides(s1)) - }) - it('rect is front/left/right', function(){ - // assert.equal(FRONT | LEFT | RIGHT, sides(s0)) + it('rect splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0)) }) - it('corner is back/left/right', function(){ -// assert.equal(BACK| LEFT | RIGHT, sides(s1)) + it('corner splits on all 4 sides', function(){ + assert.equal(ALL, sides(s1)) }) }) -- cgit v1.2.3-70-g09d2 From 5e0c67b5d70d90ea12b207e3c442378559f8f219 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Wed, 23 Jul 2014 18:51:51 -0400 Subject: basic culling tests --- Makefile | 3 + .../javascripts/rectangles/engine/rooms/_rooms.js | 6 +- .../javascripts/rectangles/engine/rooms/clipper.js | 8 ++- .../assets/javascripts/rectangles/models/room.js | 6 +- .../assets/javascripts/rectangles/models/vec2.js | 2 +- .../assets/javascripts/rectangles/util/colors.js | 10 ++- public/assets/javascripts/rectangles/util/uid.js | 12 ++-- test/00-setup.js | 2 +- test/02-test-rect.js | 22 +++++++ test/03-test-clipping.js | 77 ++++++++++++++++++++-- 10 files changed, 126 insertions(+), 22 deletions(-) (limited to 'public/assets/javascripts/rectangles/models/vec2.js') diff --git a/Makefile b/Makefile index b7e7836..877cd69 100644 --- a/Makefile +++ b/Makefile @@ -2,5 +2,8 @@ test: ./node_modules/.bin/mocha -R nyan +spec: + ./node_modules/.bin/mocha -R spec + .PHONY: test diff --git a/public/assets/javascripts/rectangles/engine/rooms/_rooms.js b/public/assets/javascripts/rectangles/engine/rooms/_rooms.js index f7fad3e..4ad3e2c 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/_rooms.js +++ b/public/assets/javascripts/rectangles/engine/rooms/_rooms.js @@ -13,8 +13,8 @@ vec2 = require('../../models/vec2') Rect = require('../../models/rect') Room = require('../../models/room') - sort = require('../../util/sort') UidGenerator = require('../../util/uid') + sort = require('../../util/sort') _ = require('lodash') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 TOP = CEILING, BOTTOM = FLOOR @@ -38,6 +38,8 @@ base.walls = {} base.regions = [] + UidGenerator.setList(base.list) + base.init = function(){ Rooms.builder.init() Rooms.clipper.init() @@ -116,8 +118,6 @@ return [] } - base.uid = UidGenerator(base.list) - base.sorted_by_position = function(){ return sort.rooms_by_position( base.values() ) } diff --git a/public/assets/javascripts/rectangles/engine/rooms/clipper.js b/public/assets/javascripts/rectangles/engine/rooms/clipper.js index cd45479..365ae8c 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/clipper.js +++ b/public/assets/javascripts/rectangles/engine/rooms/clipper.js @@ -41,7 +41,7 @@ app.tube("clip") } - var rooms, regions + var regions // Given a set of overlapping rooms, clip any intersections, then cull any duplicate polygons base.solve_rects = function(){ @@ -59,6 +59,7 @@ // Reset the clipping/culling states of each of the rooms base.reset_rects = function(){ + regions = [] Rooms.forEach(function(room){ room.reset() }) @@ -67,7 +68,6 @@ // Compare each room to the rooms it overlaps, and subdivide base.clip_rects = function(){ var rooms = Rooms.sorted_by_position() - regions = [] var left, right for (var i = 0; i < rooms.length; i++) { @@ -89,6 +89,8 @@ rooms[i].regions = rooms[i].regions.filter(function(r){ return !!r }) regions = regions.concat(rooms[i].regions) } + + return regions } // Find overlapping regions (of the same size) and dedupe @@ -125,6 +127,8 @@ ttx.data = new Tree (regions[i].y.a, [regions[i]]) } } + + return regions } return base diff --git a/public/assets/javascripts/rectangles/models/room.js b/public/assets/javascripts/rectangles/models/room.js index 748b244..e5f42fe 100644 --- a/public/assets/javascripts/rectangles/models/room.js +++ b/public/assets/javascripts/rectangles/models/room.js @@ -1,14 +1,16 @@ (function(){ - var vec2, Rect, sort + var vec2, Rect, UidGenerator, sort if ('window' in this) { vec2 = window.vec2 Rect = window.Rect + UidGenerator = window.UidGenerator sort = window.sort } else { vec2 = require('./vec2') Rect = require('./rect') + UidGenerator = require('../util/uid') sort = require('../util/sort') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 TOP = CEILING, BOTTOM = FLOOR @@ -25,7 +27,7 @@ } var Room = function(opt){ - this.id = opt.id || Rooms.uid("room_") + this.id = opt.id || UidGenerator("room_") this.rect = opt.rect this.regions = [] this.walls = [] diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 5c2b519..b0c88c1 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -7,7 +7,7 @@ return this.b-this.a } vec2.prototype.length = function(){ - return abs(this.b-this.a) + return Math.abs(this.b-this.a) } vec2.prototype.dist = function(){ return dist(0,this.a,0,this.b) diff --git a/public/assets/javascripts/rectangles/util/colors.js b/public/assets/javascripts/rectangles/util/colors.js index c590072..95827cc 100644 --- a/public/assets/javascripts/rectangles/util/colors.js +++ b/public/assets/javascripts/rectangles/util/colors.js @@ -3,6 +3,12 @@ alpha: [ "rgba(0,0,0,0.1)", ], + alphaQuad: [ + "rgba(0,0,0,0.1)", + "rgba(0,0,0,0.1)", + "rgba(0,0,0,0.1)", + "rgba(0,0,0,0.1)", + ], redblue: [ "rgba(0,0,0,0.2)", "rgba(255,0,0,0.2)", @@ -52,9 +58,9 @@ select.blur() }) - window.colors = color_palettes[select ? select.value : 'bone'] + window.colors = color_palettes[select ? select.value : 'alphaQuad'] window.grayColors = {} - _.zip([FRONT, LEFT, BACK, RIGHT], color_palettes.bone).map(function(pair){ + _.zip([FRONT, LEFT, BACK, RIGHT], color_palettes.alphaQuad).map(function(pair){ window.grayColors[pair[0]] = pair[1] }) window.palettes = color_palettes diff --git a/public/assets/javascripts/rectangles/util/uid.js b/public/assets/javascripts/rectangles/util/uid.js index 648bf0c..50d18c5 100644 --- a/public/assets/javascripts/rectangles/util/uid.js +++ b/public/assets/javascripts/rectangles/util/uid.js @@ -1,9 +1,8 @@ - (function(){ var UidGenerator = function(list){ var id = 0 - return function(s){ + var generator = function(s){ s = s || "" var ss while (1) { @@ -13,13 +12,18 @@ } } } + generator.setList = function(newList){ + list = newList + } + return generator } if ('window' in this) { - window.UidGenerator = UidGenerator + window.UidGenerator = new UidGenerator } else { - module.exports = UidGenerator + module.exports = new UidGenerator } })() + diff --git a/test/00-setup.js b/test/00-setup.js index 8d06a13..78ad2c4 100644 --- a/test/00-setup.js +++ b/test/00-setup.js @@ -1,2 +1,2 @@ -Error.stackTraceLimit = 1 +Error.stackTraceLimit = 5 diff --git a/test/02-test-rect.js b/test/02-test-rect.js index 55f1ec9..29998da 100644 --- a/test/02-test-rect.js +++ b/test/02-test-rect.js @@ -183,6 +183,28 @@ describe('rect', function(){ it('corner splits on all 4 sides', function(){ assert.equal(ALL, sides(s1)) }) + + var rect_map = {} + var corner_map = {} + var rect_state = s0.forEach(function(r){ + rect_map[r.sides] = rect_map[r.sides] || [] + rect_map[r.sides].push(r) + }) + var corner_state = s1.forEach(function(r){ + corner_map[r.sides] = corner_map[r.sides] || [] + corner_map[r.sides].push(r) + }) + + it('rect contains a rect with no sides', function(){ + assert.equal(1, rect_map[0].length) + }) + it('corner contains a rect with no sides', function(){ + assert.equal(1, corner_map[0].length) + }) + it('rect and corner overlap', function(){ + assert.equal(String(rect_map[0][0]), String(corner_map[0][0])) + }) + }) }) diff --git a/test/03-test-clipping.js b/test/03-test-clipping.js index 1668bd1..4743eb0 100644 --- a/test/03-test-clipping.js +++ b/test/03-test-clipping.js @@ -1,5 +1,5 @@ var assert = require("assert") -var vec2 = require("../public/assets/javascripts/rectangles/models/vec2.js") +var vec = require("../public/assets/javascripts/rectangles/models/vec2.js") var Rect = require("../public/assets/javascripts/rectangles/models/rect.js") var Room = require("../public/assets/javascripts/rectangles/models/room.js") var Rooms = require("../public/assets/javascripts/rectangles/engine/rooms/_rooms.js") @@ -7,15 +7,78 @@ var Clipper = require("../public/assets/javascripts/rectangles/engine/rooms/clip var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 var ALL = FRONT | BACK | LEFT | RIGHT +var rect = new Rect( new vec(1,4), new vec(1,4) ) +var east = new Rect( new vec(2,5), new vec(1,4) ) +var corner = new Rect( new vec(3,5), new vec(3,5) ) + +function report(a) { + console.log( a.join("\n") ) +} + describe('clipper', function(){ - describe('#intersects()', function(){ - // var rect = new Rect(0, 0, 10, 10) + Rooms.list = {} + Rooms.regions = [] + Rooms.add_with_rect( rect ) + Rooms.add_with_rect( east ) + + describe('#clip_rects(rect, east)', function(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() - /* - it('intersects itself', function(){ - assert.equal(true, rect.intersects( new Rect(0, 0, 10, 10) )); + it('contains duplicates', function(){ + var map = {} + var state = regions.some(function(a){ + var s = a.toString() + if (s in map) return true + map[s] = s + return false + }) + assert.equal(true, state) + }) + }) + + describe('#cull_rects(rect, east)', function(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + var culled = Rooms.clipper.cull_rects() + var culled_dupes = culled.filter(function(r){ return r.dupe }) + var culled_regions = culled.filter(function(r){ return ! r.dupe }) + + it('clipper returns 4 rects', function(){ + assert.equal(4, regions.length) + }) + it('culling marks 1 duplicate', function(){ + assert.equal(1, culled_dupes.length) + }) + it('culling marks 3 non-duplicate', function(){ + assert.equal(3, culled_regions.length) }) - */ + }) + + // + + Rooms.list = {} + Rooms.regions = [] + Rooms.add_with_rect( rect ) + Rooms.add_with_rect( corner ) + describe('#cull_rects(rect, corner)', function(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + var culled = Rooms.clipper.cull_rects() + var culled_dupes = culled.filter(function(r){ return r.dupe }) + var culled_regions = culled.filter(function(r){ return ! r.dupe }) + + it('clipper returns 8 rects', function(){ + assert.equal(8, regions.length) + }) + it('culling marks 1 duplicate', function(){ + assert.equal(1, culled_dupes.length) + }) + it('culling marks 7 non-duplicate', function(){ + assert.equal(7, culled_regions.length) + }) }) + }) + -- cgit v1.2.3-70-g09d2 From 9261438f86b1faf22a0f8d9a366fb0daa3dd090d Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 24 Jul 2014 14:45:22 -0400 Subject: iterative culling algorithm --- .../javascripts/rectangles/engine/rooms/clipper.js | 22 +++++++++++++++++- .../assets/javascripts/rectangles/models/rect.js | 3 +++ .../assets/javascripts/rectangles/models/vec2.js | 13 +++++++++++ public/assets/javascripts/rectangles/util/sort.js | 7 +++++- test/03-test-clipping.js | 26 ++++++++++++++++++---- 5 files changed, 65 insertions(+), 6 deletions(-) (limited to 'public/assets/javascripts/rectangles/models/vec2.js') diff --git a/public/assets/javascripts/rectangles/engine/rooms/clipper.js b/public/assets/javascripts/rectangles/engine/rooms/clipper.js index 365ae8c..d67f6ad 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/clipper.js +++ b/public/assets/javascripts/rectangles/engine/rooms/clipper.js @@ -52,7 +52,7 @@ base.reset_rects() base.clip_rects() - base.cull_rects() + base.cull_rects_iterative() Rooms.regions = sort.rects_by_position(regions) } @@ -131,6 +131,26 @@ return regions } + // Find overlapping regions and dedupe the smaller ones + base.cull_rects_iterative = function(){ + regions = sort.rects_by_area( regions ) + + var region_i, region_j, i, j, _len + + for (i = 0, _len = regions.length; i < _len-1; i++) { + region_i = regions[i] + for (j = i+1; j < _len; j++) { + region_j = regions[j] + if (region_j.dupe) continue; + if (region_i.overlaps(region_j)) { + region_i.dupe = true + } + } + } + + return regions + } + return base } diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 3341239..590440a 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -94,6 +94,9 @@ Rect.prototype.containsDisc = function(x,y,r){ return this.x.containsDisc(x,r) && this.y.containsDisc(y,r) } + Rect.prototype.overlaps = function(rect){ + return this.x.overlaps(rect.x) && this.y.overlaps(rect.y) + } Rect.prototype.intersects = function(r){ var corner_intersect = (this.x.b === r.x.a && this.y.b === r.y.a) return this.x.intersects(r.x) && this.y.intersects(r.y) && ! corner_intersect diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index b0c88c1..ee02088 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -93,6 +93,19 @@ return (this.a < v.b && v.b <= this.b) || (v.a < this.b && this.b <= v.b) } } + vec2.prototype.overlaps = function(v){ + if (this.a == v.a || this.b == v.b) { + return true + } + if (this.a < v.a) { + return (v.a < this.b && this.b < v.b) || (this.a < v.b && v.b < this.b) + } + else if (v.a < this.a) { + return (this.a < v.b && v.b < this.b) || (v.a < this.b && this.b < v.b) + } + } + + vec2.prototype.adjacent = function(v){ if (this.a == v.a || this.b == v.b || this.a == v.b || this.b == v.a) { return true diff --git a/public/assets/javascripts/rectangles/util/sort.js b/public/assets/javascripts/rectangles/util/sort.js index c0b5d54..3b4771c 100644 --- a/public/assets/javascripts/rectangles/util/sort.js +++ b/public/assets/javascripts/rectangles/util/sort.js @@ -39,6 +39,7 @@ function room_height_tuple (r){ return [r.height, r] } function room_area_tuple (r){ return [r.rect.area(), r] } function rect_area_tuple (r){ return [r.area(), r] } + function rect_area_tuple_larger (r){ return [-r.area(), r] } function room_rect_tuple (r){ return [r.rect, r] } function identity_tuple (r){ return [r, r] } @@ -78,7 +79,11 @@ .sort(compare_car) .map(cdr) } - + sort.rects_by_larger_area = function (list){ + return list.map(rect_area_tuple_larger) + .sort(compare_car) + .map(cdr) + } sort.compare_z = function (a,b){ return a.rect.y.a < b.rect.y.a ? -1 : a.rect.y.a == b.rect.y.a ? 0 : 1 } diff --git a/test/03-test-clipping.js b/test/03-test-clipping.js index 037b5ff..20dadb3 100644 --- a/test/03-test-clipping.js +++ b/test/03-test-clipping.js @@ -89,6 +89,8 @@ describe('clipper', function(){ Rooms.add_with_rect( corner ) Rooms.add_with_rect( peninsula ) +/* + // this method uses a tree to match areas, which tends to leave extra overlapping regions describe('#cull_rects(rect, corner, peninsula)', function(){ Rooms.clipper.reset_rects() var regions = Rooms.clipper.clip_rects() @@ -96,10 +98,6 @@ describe('clipper', function(){ var culled_dupes = culled.filter(function(r){ return r.dupe }) var culled_regions = culled.filter(function(r){ return ! r.dupe }) - report(culled_dupes) - report([]) - report(culled_regions) - it('clipper returns 16 rects', function(){ assert.equal(16, regions.length) }) @@ -110,6 +108,26 @@ describe('clipper', function(){ assert.equal(14, culled_regions.length) }) }) +*/ + + // this method iterates to match areas, which omits regions in some cases + describe('#cull_rects_iterative(rect, corner, peninsula)', function(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + var culled = Rooms.clipper.cull_rects_iterative() + var culled_dupes = culled.filter(function(r){ return r.dupe }) + var culled_regions = culled.filter(function(r){ return ! r.dupe }) + + it('clipper returns 16 rects', function(){ + assert.equal(16, regions.length) + }) + it('culling marks 3 duplicate', function(){ + assert.equal(3, culled_dupes.length) + }) + it('culling marks 14 non-duplicate', function(){ + assert.equal(13, culled_regions.length) + }) + }) }) -- cgit v1.2.3-70-g09d2 From d18cb4c622fbb0172b9618d8594953a32ccb88b2 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 29 Jul 2014 17:46:40 -0400 Subject: fix wall clamping bug --- .../javascripts/rectangles/engine/rooms/mover.js | 11 +--- .../assets/javascripts/rectangles/models/room.js | 10 +-- .../assets/javascripts/rectangles/models/vec2.js | 2 + test/01-test-vec2.js | 57 +++++++++++++++-- test/05-test-mover.js | 74 ++++++++++++++++++++++ 5 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 test/05-test-mover.js (limited to 'public/assets/javascripts/rectangles/models/vec2.js') diff --git a/public/assets/javascripts/rectangles/engine/rooms/mover.js b/public/assets/javascripts/rectangles/engine/rooms/mover.js index e67d9bc..7195fcc 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/mover.js +++ b/public/assets/javascripts/rectangles/engine/rooms/mover.js @@ -43,15 +43,8 @@ Rooms.mover = new function(){ var collision = base.room.collidesDisc(pos.x, pos.z, radius) if (collision) { - if (! (collision & LEFT_RIGHT)) { - cam.x = base.room.rect.x.clampDisc(pos.x, radius) - } - else { - // cam.x = base.room.rect.x.clampDisc(pos.x, radius) - } - if (! (collision & FRONT_BACK)) { - cam.z = base.room.rect.y.clampDisc(pos.z, radius) - } + cam.x = (collision & LEFT_RIGHT) ? base.room.rect.x.clampDisc(pos.x, radius) : pos.x + cam.z = (collision & FRONT_BACK) ? base.room.rect.y.clampDisc(pos.z, radius) : pos.z return } diff --git a/public/assets/javascripts/rectangles/models/room.js b/public/assets/javascripts/rectangles/models/room.js index e5f42fe..32549e9 100644 --- a/public/assets/javascripts/rectangles/models/room.js +++ b/public/assets/javascripts/rectangles/models/room.js @@ -14,6 +14,8 @@ sort = require('../util/sort') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 TOP = CEILING, BOTTOM = FLOOR + FRONT_BACK = FRONT | BACK + LEFT_RIGHT = LEFT | RIGHT function sidesToString(sides){ var s = "" if (sides & FRONT) s += "front " @@ -238,12 +240,12 @@ if (contains_x) { collision |= wall_collision & FRONT_BACK } - else if (contains_y) { + if (contains_y) { collision |= wall_collision & LEFT_RIGHT } - else if (bitcount(wall_collision) > 1) { - collision |= wall_collision - } +// if (bitcount(wall_collision) > 1) { +// collision |= wall_collision +// } }) return collision } diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index ee02088..2bf286b 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -1,4 +1,6 @@ (function(){ + function clamp(n,a,b){ return n