diff options
| author | Julie Lala <jules@okfoc.us> | 2014-04-11 00:29:49 -0400 |
|---|---|---|
| committer | Julie Lala <jules@okfoc.us> | 2014-04-11 00:29:49 -0400 |
| commit | 30d75e82c7649ef2ab9f71fa360f5735eb098bc5 (patch) | |
| tree | 92f4b287e250ebfb471d871e41382b5222c0790f /rect.js | |
| parent | 218ba4f20989d05ad24a35fafda6f0d39cfa7b89 (diff) | |
shift to quantize
Diffstat (limited to 'rect.js')
| -rw-r--r-- | rect.js | 60 |
1 files changed, 60 insertions, 0 deletions
@@ -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) +} |
