Skip to content

Instantly share code, notes, and snippets.

@edp1096
Created January 21, 2022 15:27
Show Gist options
  • Save edp1096/ec8c351232c2ffb74cb81a424d8abc44 to your computer and use it in GitHub Desktop.
Save edp1096/ec8c351232c2ffb74cb81a424d8abc44 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