diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-04-11 15:20:35 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-04-11 15:20:35 -0400 |
| commit | 4da6259061060a43fe76f89fa908935536e2b666 (patch) | |
| tree | 1e531345e104524b582dd1cafc1af49bd4a00afd /rect.js | |
| parent | 94e4d463e8b485ddcd45168b4601b9b609f5f21f (diff) | |
AABB intersection, handling most cases
Diffstat (limited to 'rect.js')
| -rw-r--r-- | rect.js | 65 |
1 files changed, 54 insertions, 11 deletions
@@ -23,8 +23,8 @@ 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.x.abs().add(this.translation.a) + this.y.abs().add(this.translation.b) this.translation.a = this.translation.b = 0 return this } @@ -65,26 +65,69 @@ rect.prototype.reset = function(){ } rect.prototype.clipTo = function(r){ // for each of this rect's regions split the region if necessary + var regions = this.regions + var splits 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) + splits = regions[i].split(r) + regions = regions.concat(splits) regions[i] = null } } + this.regions = regions } rect.prototype.split = function(r){ var splits = [] + var x_intervals = [], y_intervals = [] + // 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 )) + x_intervals.push( new vec2( r.x.a, this.x.b )) + } + + 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 )) + x_intervals.push( new vec2( r.x.b, this.x.b )) + } + 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 )) + x_intervals.push( new vec2( r.x.a, r.x.b )) + x_intervals.push( new vec2( r.x.b, this.x.b )) + } + + 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 )) + } + + + // 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 )) + y_intervals.push( new vec2( r.y.a, this.y.b )) + } + + 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 )) + y_intervals.push( new vec2( r.y.b, this.y.b )) + } + + 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 )) + y_intervals.push( new vec2( r.y.a, r.y.b )) + y_intervals.push( new vec2( r.y.b, this.y.b )) + } + + 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 )) + } - // 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) + x_intervals.forEach(function(x){ + y_intervals.forEach(function(y){ + splits.push(new rect(x,y)) + }) + }) return splits } |
