diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-11-17 15:46:39 -0500 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-11-17 15:46:39 -0500 |
| commit | 5fa235a556d384117840b7088575012dcd1787dd (patch) | |
| tree | 2e819d13093cd446bdb32a2885c068da4ab94cf0 /assets/javascripts/math | |
| parent | 548ef92e8157f1ae0b594d0fd2c609438d748222 (diff) | |
polygonal lasso
Diffstat (limited to 'assets/javascripts/math')
| -rw-r--r-- | assets/javascripts/math/point.js | 9 | ||||
| -rw-r--r-- | assets/javascripts/math/vec2.js | 114 |
2 files changed, 28 insertions, 95 deletions
diff --git a/assets/javascripts/math/point.js b/assets/javascripts/math/point.js index 354be00..5a5d0db 100644 --- a/assets/javascripts/math/point.js +++ b/assets/javascripts/math/point.js @@ -5,6 +5,15 @@ this.a = a this.b = b } + + point.prototype.distanceTo = function(p){ + return Math.sqrt(this.dot(p)) + } + point.prototype.dot = function(p){ + return Math.pow(this.a - p.a, 2) + Math.pow(this.b - p.b, 2) + } + + point.prototype.magnitude = function(){ return this.b-this.a } diff --git a/assets/javascripts/math/vec2.js b/assets/javascripts/math/vec2.js index 3e1f463..550d0db 100644 --- a/assets/javascripts/math/vec2.js +++ b/assets/javascripts/math/vec2.js @@ -6,18 +6,6 @@ } else { point = require('./point') - FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 - TOP = CEILING, BOTTOM = FLOOR - function sidesToString(sides){ - var s = "" - if (sides & FRONT) s += "front " - if (sides & BACK) s += "back " - if (sides & LEFT) s += "left " - if (sides & RIGHT) s += "right " - if (sides & TOP) s += "top " - if (sides & BOTTOM) s += "bottom " - return s - } } var vec2 = function (x0,y0,x1,y1){ @@ -33,8 +21,6 @@ this.x = new point(x0,x1) this.y = new point(y0,y1) } - this.translation = new point(0,0) - this.sides = FRONT | BACK | LEFT | RIGHT } vec2.prototype.clone = function(){ return new vec2( this.x.clone(), this.y.clone() ) @@ -75,24 +61,7 @@ this.translation.a = this.translation.b = 0 return this } - vec2.prototype.resize = function(translation, sides){ - var translation = translation || this.translation - sides = sides || translation.sides - - if (sides & LEFT) { - this.x.a += translation.a - } - if (sides & RIGHT) { - this.x.b += translation.a - } - if (sides & FRONT) { - this.y.a += translation.b - } - if (sides & BACK) { - this.y.b += translation.b - } - this.translation.a = this.translation.b = 0 - } + vec2.prototype.contains = function(x,y){ return this.x.contains(x) && this.y.contains(y) } @@ -122,22 +91,22 @@ this.a.zero() this.b.zero() } - vec2.prototype.nearEdge = function (x, y, r) { - var edges = 0 - if (x < this.x.a+r) { - edges |= LEFT - } - else if (x > this.x.b-r) { - edges |= RIGHT - } - if (y < this.y.a+r) { - edges |= FRONT - } - else if (y > this.y.b-r) { - edges |= BACK - } - return edges - } +// vec2.prototype.nearEdge = function (x, y, r) { +// var edges = 0 +// if (x < this.x.a+r) { +// edges |= LEFT +// } +// else if (x > this.x.b-r) { +// edges |= RIGHT +// } +// if (y < this.y.a+r) { +// edges |= FRONT +// } +// else if (y > this.y.b-r) { +// edges |= BACK +// } +// return edges +// } vec2.prototype.width = function(){ return this.x.length() } vec2.prototype.height = function(){ return this.y.length() } vec2.prototype.delta = function(){ return new point( this.x.magnitude(), this.y.magnitude() ) } @@ -165,13 +134,11 @@ return this } vec2.prototype.toString = function(){ - var sides = sidesToString(this.sides) - var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides + var s = "[" + this.x.toString() + " " + this.y.toString() + "] " return s } vec2.prototype.exactString = function(){ - var sides = sidesToString(this.sides) - var s = "[" + this.x.exactString() + " " + this.y.exactString() + "] " + sides + var s = "[" + this.x.exactString() + " " + this.y.exactString() + "] " return s } @@ -183,49 +150,6 @@ this.y.quantize(n) return this } - vec2.prototype.split = function(r){ - var rz = this - var splits = [] - var sides = this.sides - - // bisect (or trisect) two overlapping rectangles - var x_intervals = this.x.split( r.x, LEFT, RIGHT ) - var y_intervals = this.y.split( r.y, FRONT, BACK ) - - // generate rectangular regions by crossing the two sets of vectors - x_intervals.forEach(function(x, i){ - y_intervals.forEach(function(y, i){ - var rn = new vec2(x[0], y[0]) - rn.id = rz.id - rn.sides = ((x[1] | y[1]) & sides) - rn.focused = rz.focused - splits.push(rn) - - // cull extra walls from overlapping regions - 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.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 - } - } - - }) - }) - - return splits - } if ('window' in this) { window.vec2 = vec2 |
