diff options
Diffstat (limited to 'public/assets/javascripts/rectangles/models')
| -rw-r--r-- | public/assets/javascripts/rectangles/models/rect.js | 64 | ||||
| -rw-r--r-- | public/assets/javascripts/rectangles/models/vec2.js | 39 |
2 files changed, 46 insertions, 57 deletions
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 + "]" } |
