summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models
diff options
context:
space:
mode:
authorJulie Lala <jules@okfoc.us>2014-07-23 17:03:04 -0400
committerJulie Lala <jules@okfoc.us>2014-07-23 17:27:58 -0400
commitc7e27b743eb8488ec71adaf365056ff500b458ab (patch)
treeb806895981a752368bf294edc54d283911b993d3 /public/assets/javascripts/rectangles/models
parentc3d855b3f0b6af000c0d359da6a2b774bcd0a5d5 (diff)
preparing modules for clip test
Diffstat (limited to 'public/assets/javascripts/rectangles/models')
-rw-r--r--public/assets/javascripts/rectangles/models/rect.js28
-rw-r--r--public/assets/javascripts/rectangles/models/room.js37
-rw-r--r--public/assets/javascripts/rectangles/models/tree.js85
3 files changed, 87 insertions, 63 deletions
diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js
index c14d62c..3341239 100644
--- a/public/assets/javascripts/rectangles/models/rect.js
+++ b/public/assets/javascripts/rectangles/models/rect.js
@@ -140,15 +140,20 @@
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 Rect(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
@@ -168,30 +173,9 @@
}
}
-// if (rz.x.contains(r.x.a) || rz.x.contains(r.x.b)) {
-// if (r.x.contains(rn.x.b)) {
-// rn.sides &= ~ LEFT
-// }
-// if (r.x.contains(rn.x.b)) {
-// rn.sides &= ~ RIGHT
-// }
-// }
-// if (rz.y.contains(r.y.a) || rz.y.contains(r.y.b)) {
-// if (r.y.contains(rn.y.a)) {
-// rn.sides &= ~ FRONT
-// }
-// if (r.y.contains(rn.y.b)) {
-// rn.sides &= ~ BACK
-// }
-// }
-
-// if (r.intersects(rn)) {
-// rn.sides = 0
-// }
- rn.focused = rz.focused
- splits.push(rn)
})
})
+
return splits
}
diff --git a/public/assets/javascripts/rectangles/models/room.js b/public/assets/javascripts/rectangles/models/room.js
index 61a7447..748b244 100644
--- a/public/assets/javascripts/rectangles/models/room.js
+++ b/public/assets/javascripts/rectangles/models/room.js
@@ -1,4 +1,28 @@
-window.Room = (function(){
+
+(function(){
+ var vec2, Rect, sort
+ if ('window' in this) {
+ vec2 = window.vec2
+ Rect = window.Rect
+ sort = window.sort
+ }
+ else {
+ vec2 = require('./vec2')
+ Rect = require('./rect')
+ sort = require('../util/sort')
+ 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 Room = function(opt){
this.id = opt.id || Rooms.uid("room_")
@@ -81,10 +105,10 @@ window.Room = (function(){
var side = pair[0], els = pair[1]
if (side & LEFT_RIGHT) {
- els.sort(compare_x)
+ els.sort(sort.compare_x)
}
else if (side & FRONT_BACK) {
- els.sort(compare_z)
+ els.sort(sort.compare_z)
}
// wall holds state for the last wall we created/saw..
@@ -222,7 +246,12 @@ window.Room = (function(){
return collision
}
- return Room
+ if ('window' in this) {
+ window.Room = Room
+ }
+ else {
+ module.exports = Room
+ }
})()
diff --git a/public/assets/javascripts/rectangles/models/tree.js b/public/assets/javascripts/rectangles/models/tree.js
index 8193988..7c698fe 100644
--- a/public/assets/javascripts/rectangles/models/tree.js
+++ b/public/assets/javascripts/rectangles/models/tree.js
@@ -1,37 +1,48 @@
-var Tree = function(n, data){
- this.lo = null
- this.hi = null
- this.value = n
- this.data = data
-}
-Tree.prototype.find = function(n){
- if (n == this.value) return this
- if (n < this.value) return this.lo ? this.lo.find(n) : this
- if (n > this.value) return this.hi ? this.hi.find(n) : this
-}
-Tree.prototype.add = function(n, data){
- var closest = this.find(n)
- if (n == closest.value) return closest
- if (n < closest.value) return closest.lo = new Tree(n, data)
- if (n > closest.value) return closest.hi = new Tree(n, data)
-}
-Tree.prototype.toArray = function(){
- var a = []
- if (this.lo) a = a.concat(this.lo.toArray())
- a.push(this.data)
- if (this.hi) a = a.concat(this.hi.toArray())
- return a
-}
-Tree.prototype.toString = function(){
- var s = "";
- if (this.lo) s += this.lo.toString()
- s += this.value + ","
- if (this.hi) s += this.hi.toString()
- return s
-}
-Tree.prototype.depth = function(){
- if (this.lo && this.hi) return 1 + max(this.lo.depth(), this.hi.depth())
- else if (this.lo) return 1 + this.lo.depth()
- else if (this.hi) return 1 + this.hi.depth()
- else return 0
-}
+(function(){
+
+ var Tree = function(n, data){
+ this.lo = null
+ this.hi = null
+ this.value = n
+ this.data = data
+ }
+ Tree.prototype.find = function(n){
+ if (n == this.value) return this
+ if (n < this.value) return this.lo ? this.lo.find(n) : this
+ if (n > this.value) return this.hi ? this.hi.find(n) : this
+ }
+ Tree.prototype.add = function(n, data){
+ var closest = this.find(n)
+ if (n == closest.value) return closest
+ if (n < closest.value) return closest.lo = new Tree(n, data)
+ if (n > closest.value) return closest.hi = new Tree(n, data)
+ }
+ Tree.prototype.toArray = function(){
+ var a = []
+ if (this.lo) a = a.concat(this.lo.toArray())
+ a.push(this.data)
+ if (this.hi) a = a.concat(this.hi.toArray())
+ return a
+ }
+ Tree.prototype.toString = function(){
+ var s = "";
+ if (this.lo) s += this.lo.toString()
+ s += this.value + ","
+ if (this.hi) s += this.hi.toString()
+ return s
+ }
+ Tree.prototype.depth = function(){
+ if (this.lo && this.hi) return 1 + max(this.lo.depth(), this.hi.depth())
+ else if (this.lo) return 1 + this.lo.depth()
+ else if (this.hi) return 1 + this.hi.depth()
+ else return 0
+ }
+
+ if ('window' in this) {
+ window.Tree = Tree
+ }
+ else {
+ module.exports = Tree
+ }
+
+})()