diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-06-03 16:24:10 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-06-03 16:24:28 -0400 |
| commit | 607f69c67a5b4dc72d2754192e3cdf67d0ad11d0 (patch) | |
| tree | 6556e7922c5bedb274bb1650e5dd100643a7895d /client/assets/javascripts/rectangles/models/rect.js | |
| parent | d31259291d807c851de4396921e0c26b6dd8dce2 (diff) | |
partitioning client and serveR
Diffstat (limited to 'client/assets/javascripts/rectangles/models/rect.js')
| -rw-r--r-- | client/assets/javascripts/rectangles/models/rect.js | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/client/assets/javascripts/rectangles/models/rect.js b/client/assets/javascripts/rectangles/models/rect.js new file mode 100644 index 0000000..7a2ac6f --- /dev/null +++ b/client/assets/javascripts/rectangles/models/rect.js @@ -0,0 +1,176 @@ + +window.Rect = (function(){ + var Rect = function (x0,y0,x1,y1){ + if (x0 instanceof vec2) { + this.x = x0 + this.y = y0 + } + else if (x1 === undefined) { + this.x = new vec2(x0,x0) + this.y = new vec2(y0,y0) + } + else { + this.x = new vec2(x0,x1) + this.y = new vec2(y0,y1) + } + this.translation = new vec2(0,0) + this.sides = FRONT | BACK | LEFT | RIGHT + } + Rect.prototype.clone = function(){ + return new Rect( this.x.clone(), this.y.clone() ) + } + Rect.prototype.center = function(){ + return new vec2(this.x.midpoint(), this.y.midpoint()) + } + Rect.prototype.area = function(){ + return this.x.length() * this.y.length() + } + + Rect.prototype.mul = function(n){ + this.x.mul(n) + this.y.mul(n) + return this + } + Rect.prototype.div = function(n){ + this.x.div(n) + this.y.div(n) + return this + } + + Rect.prototype.translate = function(translation){ + var translation = translation || this.translation + this.x.abs().add(translation.a) + this.y.abs().add(translation.b) + this.translation.a = this.translation.b = 0 + return this + } + Rect.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 + } + Rect.prototype.contains = function(x,y){ + return this.x.contains(x) && this.y.contains(y) + } + Rect.prototype.containsDisc = function(x,y,r){ + 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) + } + Rect.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 + } + Rect.prototype.width = function(){ return this.x.length() } + Rect.prototype.height = function(){ return this.y.length() } + Rect.prototype.toString = function(){ + var sides = sidesToString(this.sides) + var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides + return s + } + Rect.prototype.quantize = function(n){ + this.x.quantize(n) + this.y.quantize(n) + return this + } + 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 + } + + 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 + } + rn.focused = rz.focused + splits.push(rn) + }) + }) + return splits + } + + return Rect + +})() |
