summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/vec2.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/rectangles/models/vec2.js')
-rw-r--r--public/assets/javascripts/rectangles/models/vec2.js21
1 files changed, 17 insertions, 4 deletions
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