summaryrefslogtreecommitdiff
path: root/assets/javascripts/math/vec2.js
diff options
context:
space:
mode:
Diffstat (limited to 'assets/javascripts/math/vec2.js')
-rw-r--r--assets/javascripts/math/vec2.js134
1 files changed, 16 insertions, 118 deletions
diff --git a/assets/javascripts/math/vec2.js b/assets/javascripts/math/vec2.js
index 550d0db..75fd859 100644
--- a/assets/javascripts/math/vec2.js
+++ b/assets/javascripts/math/vec2.js
@@ -8,146 +8,44 @@
point = require('./point')
}
- var vec2 = function (x0,y0,x1,y1){
+ var vec2 = function (x0,y0, x1,y1){
if (x0 instanceof point) {
- this.x = x0
- this.y = y0
+ this.a = x0
+ this.b = y0
}
else if (x1 === undefined) {
- this.x = new point(x0,x0)
- this.y = new point(y0,y0)
+ this.a = new point(x0,y0)
+ this.b = new point(x0,y0)
}
else {
- this.x = new point(x0,x1)
- this.y = new point(y0,y1)
+ this.a = new point(x0,y0)
+ this.b = new point(x1,y1)
}
}
vec2.prototype.clone = function(){
- return new vec2( this.x.clone(), this.y.clone() )
- }
- vec2.prototype.assign = function(r) {
- this.x.assign(r.x)
- this.y.assign(r.y)
- return this
- }
- vec2.prototype.center = function(){
- return new point(this.x.midpoint(), this.y.midpoint())
- }
- vec2.prototype.area = function(){
- return this.x.length() * this.y.length()
- }
- vec2.prototype.magnitude = function(){
- return dist(this.x.a, this.y.a, this.x.b, this.y.b)
- }
- vec2.prototype.maxDimension = function(){
- return abs(this.width) > abs(this.height) ? this.width : this.height
+ return new vec2( this.a.clone(), this.b.clone() )
}
- vec2.prototype.mul = function(n){
- this.x.mul(n)
- this.y.mul(n)
- return this
- }
- vec2.prototype.div = function(n){
- this.x.div(n)
- this.y.div(n)
- return this
- }
+ vec2.prototype.delta = function(){ return new point( this.width(), this.height() ) }
+ vec2.prototype.width = function(){ return this.b.x - this.a.x }
+ vec2.prototype.height = function(){ return this.b.y - this.a.y }
- vec2.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
- }
- vec2.prototype.contains = function(x,y){
- return this.x.contains(x) && this.y.contains(y)
- }
- vec2.prototype.contains_point = function(p){
- return this.x.contains(p.x) && this.y.contains(p.y)
- }
- vec2.prototype.containsDisc = function(x,y,r){
- return this.x.containsDisc(x,r) && this.y.containsDisc(y,r)
- }
- vec2.prototype.overlaps = function(rect){
- return this.x.overlaps(rect.x) && this.y.overlaps(rect.y)
- }
- vec2.prototype.intersects = function(r){
- var corner_intersect = (this.x.b === r.x.a && this.y.b === r.y.a)
- return this.x.intersects(r.x) && this.y.intersects(r.y) && ! corner_intersect
- }
- vec2.prototype.adjacent = function(r){
- return this.x.adjacent(r.x) && this.y.adjacent(r.y)
- }
- vec2.prototype.eq = function(r){
- return this.x.eq(r.x) && this.y.eq(r.y)
- }
- vec2.prototype.fits = function(v){
- return this.x.length() >= v.a && this.y.length() >= v.b
- }
- vec2.prototype.zero = function(){
- this.a.zero()
- this.b.zero()
- }
-// vec2.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
-// }
- vec2.prototype.width = function(){ return this.x.length() }
- vec2.prototype.height = function(){ return this.y.length() }
- vec2.prototype.delta = function(){ return new point( this.x.magnitude(), this.y.magnitude() ) }
- vec2.prototype.expand = function(rect){
- this.x.a = Math.min( this.x.a, rect.x.a )
- this.x.b = Math.max( this.x.b, rect.x.b )
- this.y.a = Math.min( this.y.a, rect.y.a )
- this.y.b = Math.max( this.y.b, rect.y.b )
- return this
- }
- vec2.prototype.square = function(){
- var width = this.x.length()
- var height = this.y.length()
- var diff
- if (width < height) {
- diff = (height - width) / 2
- this.x.a -= diff
- this.x.b += diff
- }
- else {
- diff = (width - height) / 2
- this.y.a -= diff
- this.y.b += diff
- }
- return this
- }
vec2.prototype.toString = function(){
- var s = "[" + this.x.toString() + " " + this.y.toString() + "] "
+ var s = "[" + this.a.toString() + " " + this.b.toString() + "] "
return s
}
vec2.prototype.exactString = function(){
- var s = "[" + this.x.exactString() + " " + this.y.exactString() + "] "
+ var s = "[" + this.a.exactString() + " " + this.b.exactString() + "] "
return s
}
vec2.prototype.serialize = function(){
- return { x: this.x.serialize(), y: this.y.serialize() }
+ return { a: this.a.serialize(), b: this.b.serialize() }
}
vec2.prototype.quantize = function(n){
- this.x.quantize(n)
- this.y.quantize(n)
+ this.a.quantize(n)
+ this.b.quantize(n)
return this
}