From 30d75e82c7649ef2ab9f71fa360f5735eb098bc5 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 11 Apr 2014 00:29:49 -0400 Subject: shift to quantize --- rect.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 rect.js (limited to 'rect.js') diff --git a/rect.js b/rect.js new file mode 100644 index 0000000..dcf2d33 --- /dev/null +++ b/rect.js @@ -0,0 +1,60 @@ +function rect (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) +} +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.normalize = function(){ + this.x.normalize().add(this.translation.a) + this.y.normalize().add(this.translation.b) + this.translation.a = this.translation.b = 0 + return this +} +rect.prototype.contains = function(x,y){ + return this.x.contains(x) && this.y.contains(y) +} +rect.prototype.intersects = function(r){ + return this.x.intersects(r.x) && this.y.intersects(r.y) +} +rect.prototype.width = function(){ return this.x.length() } +rect.prototype.height = function(){ return this.y.length() } +rect.prototype.fill = function(){ + ctx.fillRect(this.x.a + this.translation.a, this.y.a + this.translation.b, this.x.length(), this.y.length()) +} +rect.prototype.stroke = function(){ + ctx.beginPath() + ctx.moveTo(this.x.a, this.y.a) + ctx.lineTo(this.x.b, this.y.b) + ctx.stroke() +} +rect.prototype.perimeter = function(){ + line( this.x.a, this.y.a, this.x.b, this.y.a, this.translation ) + line( this.x.a, this.y.b, this.x.b, this.y.b, this.translation ) + line( this.x.a, this.y.a, this.x.a, this.y.b, this.translation ) + line( this.x.b, this.y.a, this.x.b, this.y.b, this.translation ) +} +rect.prototype.toString = function(){ + return "[" + this.x.toString() + " " + this.y.toString() + "]" +} +rect.prototype.quantize = function(n){ + this.x.quantize(n) + this.y.quantize(n) +} -- cgit v1.2.3-70-g09d2 From 6f895c8db89ce8ae6295db1dc93284cdb521217c Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 11 Apr 2014 10:20:57 -0400 Subject: notes --- rect.js | 30 ++++++++++++++++++++++++++++++ rectangles.html | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) (limited to 'rect.js') diff --git a/rect.js b/rect.js index dcf2d33..f643b6f 100644 --- a/rect.js +++ b/rect.js @@ -58,3 +58,33 @@ rect.prototype.quantize = function(n){ this.x.quantize(n) this.y.quantize(n) } +rect.prototype.reset = function(){ + var copy = this.clone() + copy.sides = 0xf + this.regions = [ copy ] +} +rect.prototype.clipTo = function(r){ + // for each of this rect's regions split the region if necessary + for (var i = 0, len = regions.length; i < len; i++) { + if (regions[i] && regions[i].intersects(r)) { + var splits = regions[i].split(r) + regions.concat(splits) + regions[i] = null + } + } +} +rect.prototype.split = function(r){ + var splits = [] + + + + // 1 split (horizontal) + // 1 split (vertical) + // 2 splits (top left) + // 2 splits (top right) + // 2 splits (bottom left) + // 2 splits (bottom right) + // 3 splits (center) + + return splits +} diff --git a/rectangles.html b/rectangles.html index aa31344..7acc52b 100644 --- a/rectangles.html +++ b/rectangles.html @@ -141,7 +141,6 @@ function solve_rects(){ var left, right for (var i = 0; i < rects.length; i++) { left = rects[i] - left.regions = [] for (var j = 0; j < rects.length; j++) { right = rects[j] if (left.intersects(right)) { @@ -156,6 +155,7 @@ function solve_rects(){ // for (var i = 0; i < rects.length; i++) { // regions.concat(rect[i].regions) // } + // handle when two walls are coplanar // generate floor and ceiling for some regions // generate walls from surviving regions // generate ceiling-walls where ceiling has discontinuity -- cgit v1.2.3-70-g09d2