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.js89
1 files changed, 81 insertions, 8 deletions
diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js
index 9233aec..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<a?a:n<b?n:b }
+
var vec2 = function (a,b){
this.a = a
this.b = b
@@ -7,7 +9,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)
@@ -70,6 +72,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
}
@@ -80,16 +85,35 @@
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.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
+ }
+ return false
+ }
vec2.prototype.union = function(v){
if (this.intersects(v)) {
return new vec2( min(this.a,v.a), max(this.b, v.b) )
@@ -100,6 +124,56 @@
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 = [], _len
+
+ 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 ), 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 ), left ])
+ 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 ), 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
+ }
+
vec2.prototype.toString = function(){
return "[" + ~~this.a + " " + ~~this.b + "]"
}
@@ -115,8 +189,7 @@
if ('window' in this) {
window.vec2 = vec2
}
- else if ('module' in this) {
+ else {
module.exports = vec2
}
-
-})() \ No newline at end of file
+})()