Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple Vector class in javascript
var Vector = function(x, y) {
this.x = x || 0;
this.y = y || 0;
};
// return the angle of the vector in radians
Vector.prototype.getDirection = function() {
return Math.atan2(this.y, this.x);
};
// set the direction of the vector in radians
Vector.prototype.setDirection = function(direction) {
var magnitude = this.getMagnitude();
this.x = Math.cos(angle) * magnitude;
this.y = Math.sin(angle) * magnitude;
};
// get the magnitude of the vector
Vector.prototype.getMagnitude = function() {
// use pythagoras theorem to work out the magnitude of the vector
return Math.sqrt(this.x * this.x + this.y * this.y);
};
// set the magnitude of the vector
Vector.prototype.setMagnitude = function(magnitude) {
var direction = this.getDirection();
this.x = Math.cos(direction) * magnitude;
this.y = Math.sin(direction) * magnitude;
};
// add two vectors together and return a new one
Vector.prototype.add = function(v2) {
return new Vector(this.x + v2.x, this.y + v2.y);
};
// add a vector to this one
Vector.prototype.addTo = function(v2) {
this.x += v2.x;
this.y += v2.y;
};
// subtract two vectors and reutn a new one
Vector.prototype.subtract = function(v2) {
return new Vector(this.x - v2.x, this.y - v2.y);
};
// subtract a vector from this one
Vector.prototype.subtractFrom = function(v2) {
this.x -= v2.x;
this.y -= v2.y;
};
// multiply this vector by a scalar and return a new one
Vector.prototype.multiply = function(scalar) {
return new Vector(this.x * scalar, this.y * scalar);
};
// multiply this vector by the scalar
Vector.prototype.multiplyBy = function(scalar) {
this.x *= scalar;
this.y *= scalar;
};
// scale this vector by scalar and return a new vector
Vector.prototype.divide = function(scalar) {
return new Vector(this.x / scalar, this.y / scalar);
};
// scale this vector by scalar
Vector.prototype.divideBy = function(scalar) {
this.x /= scalar;
this.y /= scalar;
};
// Aliases
Vector.prototype.getLength = Vector.prototype.getMagnitude;
Vector.prototype.setLength = Vector.prototype.setMagnitude;
Vector.prototype.getAngle = Vector.prototype.getDirection;
Vector.prototype.setAngle = Vector.prototype.setDirection;
// Utilities
Vector.prototype.copy = function() {
return new Vector(this.x, this.y);
};
Vector.prototype.toString = function() {
return 'x: ' + this.x + ', y: ' + this.y;
};
Vector.prototype.toArray = function() {
return [this.x, this.y];
};
Vector.prototype.toObject = function() {
return {x: this.x, y: this.y};
};
// To add
// Scale
// Normalise
// Dot?
@raonioliver
Copy link

raonioliver commented Jan 11, 2018

// dot product of two vectors
Vector.prototype.dotProduct = function(v2) {
	return this.x * v2.x + this.y *v2.y;
}

// normalize a given vector
Vector.prototype.normalize = function(){
	return new Vector(this.x/(Math.sqrt(this.x * this.x + this.y * this.y)), this.y/(Math.sqrt(this.x * this.x + this.y * this.y)));
}

I didn't test those two functions yet, and I don't know how does this site work. Sorry if I'm bothering,

@rmkane
Copy link

rmkane commented Mar 13, 2018

I think you forgot to change the name of the parameter?

// Set the direction of the vector in radians
Vector.prototype.setDirection = function(direction) {
    var magnitude = this.getMagnitude();
    this.x = Math.cos(angle) * magnitude;
    this.y = Math.sin(angle) * magnitude;
};

Checkout: Gist: winduptoy / Vector2D.js and Victor JS.

@eduardmathmed
Copy link

eduardmathmed commented Feb 12, 2020

You can also put angleD directly in the function call

// return the angle of the vector in degrees
Vector.prototype.getDirectionD = function() {
return Math.atan2(this.y, this.x) * 180 / Math.PI;
};

// set the direction of the vector in degrees
Vector.prototype.setDirectionD = function(directionD) {
var angleD = directionD;
var magnitude = this.getMagnitude();
this.x = Math.cos(angleD * 180 / Math.PI) * magnitude;
this.y = Math.sin(angleD * 180 / Math.PI) * magnitude;
};

@dr-NHA
Copy link

dr-NHA commented Sep 19, 2021

//reposted together
//And Fixed as y'all Lazy

//Setuo Vector Function
var Vector = function(x, y) {this.x = x || 0;this.y = y || 0;};

// return the angle of the vector in radians
Vector.prototype.getDirection = function() {return Math.atan2(this.y, this.x);};

// set the direction of the vector in radians
Vector.prototype.setDirection = function(angle) {var magnitude = this.getMagnitude(); this.x = Math.cos(angle) * magnitude; this.y = Math.sin(angle) * magnitude;};

// get the magnitude of the vector
// use pythagoras theorem to work out the magnitude of the vector
Vector.prototype.getMagnitude = function() {return Math.sqrt(this.x * this.x + this.y * this.y);};

// set the magnitude of the vector
Vector.prototype.setMagnitude = function(magnitude) { var direction = this.getDirection(); this.x = Math.cos(direction) * magnitude; this.y = Math.sin(direction) * magnitude;};

// add two vectors together and return a new one
Vector.prototype.add = function(v2) {return new Vector(this.x + v2.x, this.y + v2.y);};

// add a vector to this one
Vector.prototype.addTo = function(v2) {this.x += v2.x; this.y += v2.y;};

// subtract two vectors and reutn a new one
Vector.prototype.subtract = function(v2) {return new Vector(this.x - v2.x, this.y - v2.y);};

// subtract a vector from this one
Vector.prototype.subtractFrom = function(v2) { this.x -= v2.x; this.y -= v2.y;};

// multiply this vector by a scalar and return a new one
Vector.prototype.multiply = function(scalar) {return new Vector(this.x * scalar, this.y * scalar);};

// multiply this vector by the scalar
Vector.prototype.multiplyBy = function(scalar) {this.x *= scalar; this.y *= scalar;};

// scale this vector by scalar and return a new vector
Vector.prototype.divide = function(scalar) { return new Vector(this.x / scalar, this.y / scalar);};

// scale this vector by scalar
Vector.prototype.divideBy = function(scalar) { this.x /= scalar; this.y /= scalar;};

// Aliases
Vector.prototype.getLength = Vector.prototype.getMagnitude;
Vector.prototype.setLength = Vector.prototype.setMagnitude;

Vector.prototype.getAngle = Vector.prototype.getDirection;
Vector.prototype.setAngle = Vector.prototype.setDirection;

// Utilities
Vector.prototype.copy = function() {return new Vector(this.x, this.y);};

Vector.prototype.toString = function() { return 'x: ' + this.x + ', y: ' + this.y;};

Vector.prototype.toArray = function() { return [this.x, this.y];};

Vector.prototype.toObject = function() { return {x: this.x, y: this.y};};

// dot product of two vectors
Vector.prototype.dotProduct = function(v2) { return this.x * v2.x + this.y *v2.y;}

// normalize a given vector
Vector.prototype.normalize = function(){return new Vector(this.x/(Math.sqrt(this.x * this.x + this.y * this.y)), this.y/(Math.sqrt(this.x * this.x + this.y * this.y)));}

// To add
// Scale

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment