Skip to content

Instantly share code, notes, and snippets.

@jeantimex
Created July 29, 2020 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeantimex/7fa22744e0c45e5df770b532f696af2d to your computer and use it in GitHub Desktop.
Save jeantimex/7fa22744e0c45e5df770b532f696af2d to your computer and use it in GitHub Desktop.
JavaScript 2D Vector Class
/*
Simple 2D JavaScript Vector Class
Hacked from evanw's lightgl.js
https://github.com/evanw/lightgl.js/blob/master/src/vector.js
*/
function Vector(x, y) {
this.x = x || 0;
this.y = y || 0;
}
/* INSTANCE METHODS */
Vector.prototype = {
negative: function() {
this.x = -this.x;
this.y = -this.y;
return this;
},
add: function(v) {
if (v instanceof Vector) {
this.x += v.x;
this.y += v.y;
} else {
this.x += v;
this.y += v;
}
return this;
},
subtract: function(v) {
if (v instanceof Vector) {
this.x -= v.x;
this.y -= v.y;
} else {
this.x -= v;
this.y -= v;
}
return this;
},
multiply: function(v) {
if (v instanceof Vector) {
this.x *= v.x;
this.y *= v.y;
} else {
this.x *= v;
this.y *= v;
}
return this;
},
divide: function(v) {
if (v instanceof Vector) {
if(v.x != 0) this.x /= v.x;
if(v.y != 0) this.y /= v.y;
} else {
if(v != 0) {
this.x /= v;
this.y /= v;
}
}
return this;
},
equals: function(v) {
return this.x == v.x && this.y == v.y;
},
dot: function(v) {
return this.x * v.x + this.y * v.y;
},
cross: function(v) {
return this.x * v.y - this.y * v.x
},
length: function() {
return Math.sqrt(this.dot(this));
},
normalize: function() {
return this.divide(this.length());
},
min: function() {
return Math.min(this.x, this.y);
},
max: function() {
return Math.max(this.x, this.y);
},
toAngles: function() {
return -Math.atan2(-this.y, this.x);
},
angleTo: function(a) {
return Math.acos(this.dot(a) / (this.length() * a.length()));
},
toArray: function(n) {
return [this.x, this.y].slice(0, n || 2);
},
clone: function() {
return new Vector(this.x, this.y);
},
set: function(x, y) {
this.x = x; this.y = y;
return this;
}
};
/* STATIC METHODS */
Vector.negative = function(v) {
return new Vector(-v.x, -v.y);
};
Vector.add = function(a, b) {
if (b instanceof Vector) return new Vector(a.x + b.x, a.y + b.y);
else return new Vector(a.x + b, a.y + b);
};
Vector.subtract = function(a, b) {
if (b instanceof Vector) return new Vector(a.x - b.x, a.y - b.y);
else return new Vector(a.x - b, a.y - b);
};
Vector.multiply = function(a, b) {
if (b instanceof Vector) return new Vector(a.x * b.x, a.y * b.y);
else return new Vector(a.x * b, a.y * b);
};
Vector.divide = function(a, b) {
if (b instanceof Vector) return new Vector(a.x / b.x, a.y / b.y);
else return new Vector(a.x / b, a.y / b);
};
Vector.equals = function(a, b) {
return a.x == b.x && a.y == b.y;
};
Vector.dot = function(a, b) {
return a.x * b.x + a.y * b.y;
};
Vector.cross = function(a, b) {
return a.x * b.y - a.y * b.x;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment