Skip to content

Instantly share code, notes, and snippets.

@valmormn
Forked from jjgrainger/Vector.js
Created February 21, 2018 14:20
Show Gist options
  • Save valmormn/b9a6dbf41bca71f407a43fa61d0954a3 to your computer and use it in GitHub Desktop.
Save valmormn/b9a6dbf41bca71f407a43fa61d0954a3 to your computer and use it in GitHub Desktop.

Revisions

  1. @jjgrainger jjgrainger revised this gist Oct 4, 2016. No changes.
  2. @jjgrainger jjgrainger revised this gist Oct 4, 2016. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions Vector.js
    Original file line number Diff line number Diff line change
    @@ -79,6 +79,23 @@ 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
  3. @jjgrainger jjgrainger revised this gist Oct 4, 2016. 1 changed file with 35 additions and 3 deletions.
    38 changes: 35 additions & 3 deletions Vector.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    var Vector = function(x, y) {
    this.x = x || 0;
    this.x = x || 0;
    this.y = y || 0;
    };

    @@ -45,9 +45,41 @@ Vector.prototype.subtract = function(v2) {
    };

    // subtract a vector from this one
    Vector.prototype.subtracFrom = function(v2) {
    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;

    // To add
    // Scale
    // Normalise
    // Dot?
  4. @jjgrainger jjgrainger created this gist Oct 4, 2016.
    53 changes: 53 additions & 0 deletions Vector.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    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.subtracFrom = function(v2) {
    this.x -= v2.x;
    this.y -= v2.y;
    };

    //