UNPKG

vector2d

Version:

2D Vector library offering Float32Array, Array or standard Object based vectors.

297 lines 7.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * These values are used by the `AbstractVector.round` method to increase * performance vs. using Number.toFixed. */ var precision = [ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000 ]; /** * The class that all other vector representations are derived from. * * Contains the core implementation for all methods that will be exposed by * vector instances. * * Example of creating a custom implementation: * * ```ts * import { AbstractVector } from "./AbstractVector" * * export class MyCustomVector extends AbstractVector { * constructor (x: number, y: number) { * super(CustomVectorType) * } * } * ``` */ var AbstractVector = /** @class */ (function () { function AbstractVector(ctor) { this.ctor = ctor; } /** * Set both x and y axis value * @param x New x val * @param y New y val */ AbstractVector.prototype.setAxes = function (x, y) { this.x = x; this.y = y; return this; }; /** * Getter for x the axis value */ AbstractVector.prototype.getX = function () { return this.x; }; /** * Setter for x axis value */ AbstractVector.prototype.setX = function (x) { this.x = x; return this; }; /** * Getter for y axis value */ AbstractVector.prototype.getY = function () { return this.y; }; /** * Setter for y axis. */ AbstractVector.prototype.setY = function (y) { this.y = y; return this; }; /** * Return the vector as a formatted string, e.g "(0, 4)" */ AbstractVector.prototype.toString = function (round) { if (round === void 0) { round = false; } if (round) { return "(" + Math.round(this.x) + ", " + Math.round(this.y) + ")"; } return "(" + this.x + ", " + this.y + ")"; }; /** * Return an Array containing the vector axes, e.g [0, 4] */ AbstractVector.prototype.toArray = function () { return [this.x, this.y]; }; /** * Return an Object containing the vector axes, e.g { x: 0, y: 4 } */ AbstractVector.prototype.toObject = function () { return { x: this.x, y: this.y }; }; /** * Add the provided vector to this one */ AbstractVector.prototype.add = function (vec) { this.x += vec.x; this.y += vec.y; return this; }; /** * Subtract the provided vector from this one */ AbstractVector.prototype.subtract = function (vec) { this.x -= vec.x; this.y -= vec.y; return this; }; /** * Check if the provided vector equal to this one */ AbstractVector.prototype.equals = function (vec) { return vec.x === this.x && vec.y === this.y; }; /** * Multiply this vector by the provided vector */ AbstractVector.prototype.multiplyByVector = function (vec) { this.x *= vec.x; this.y *= vec.y; return this; }; /** * Multiply this vector by the provided vector */ AbstractVector.prototype.mulV = function (vec) { return this.multiplyByVector(vec); }; /** * Divide this vector by the provided vector */ AbstractVector.prototype.divideByVector = function (vec) { this.x /= vec.x; this.y /= vec.y; return this; }; /** * Divide this vector by the provided vector */ AbstractVector.prototype.divV = function (v) { return this.divideByVector(v); }; /** * Multiply this vector by the provided number */ AbstractVector.prototype.multiplyByScalar = function (n) { this.x *= n; this.y *= n; return this; }; /** * Multiply this vector by the provided number */ AbstractVector.prototype.mulS = function (n) { return this.multiplyByScalar(n); }; /** * Divive this vector by the provided number */ AbstractVector.prototype.divideByScalar = function (n) { this.x /= n; this.y /= n; return this; }; /** * Divive this vector by the provided number */ AbstractVector.prototype.divS = function (n) { return this.divideByScalar(n); }; /** * Normalise this vector */ AbstractVector.prototype.normalise = function () { return this.divideByScalar(this.magnitude()); }; /** * For American spelling. Same as unit/normalise function */ AbstractVector.prototype.normalize = function () { return this.normalise(); }; /** * The same as normalise and normalize */ AbstractVector.prototype.unit = function () { return this.normalise(); }; /** * Returns the magnitude (length) of this vector */ AbstractVector.prototype.magnitude = function () { var x = this.x; var y = this.y; return Math.sqrt(x * x + y * y); }; /** * Returns the magnitude (length) of this vector */ AbstractVector.prototype.length = function () { return this.magnitude(); }; /** * Returns the squred length of this vector */ AbstractVector.prototype.lengthSq = function () { var x = this.x; var y = this.y; return x * x + y * y; }; /** * Returns the dot product of this vector by another */ AbstractVector.prototype.dot = function (vec) { return vec.x * this.x + vec.y * this.y; }; /** * Returns the cross product of this vector by another. */ AbstractVector.prototype.cross = function (vec) { return this.x * vec.y - this.y * vec.x; }; /** * Reverses this vector i.e multiplies it by -1 */ AbstractVector.prototype.reverse = function () { this.x = -this.x; this.y = -this.y; return this; }; /** * Set the vector axes values to absolute values */ AbstractVector.prototype.abs = function () { this.x = Math.abs(this.x); this.y = Math.abs(this.y); return this; }; /** * Zeroes the vector i.e sets all axes to 0 */ AbstractVector.prototype.zero = function () { this.x = this.y = 0; return this; }; /** * Returns the distance between this vector and another */ AbstractVector.prototype.distance = function (v) { var x = this.x - v.x; var y = this.y - v.y; return Math.sqrt(x * x + y * y); }; /** * Rotates the vetor by provided radians */ AbstractVector.prototype.rotate = function (rads) { var cos = Math.cos(rads); var sin = Math.sin(rads); var ox = this.x; var oy = this.y; this.x = ox * cos - oy * sin; this.y = ox * sin + oy * cos; return this; }; /** * Rounds this vector to n decimal places */ AbstractVector.prototype.round = function (n) { if (n === void 0) { n = 2; } var p = precision[n]; // This performs waaay better than toFixed and give Float32 the edge again. // http://www.dynamicguru.com/javascript/round-numbers-with-precision/ this.x = ((0.5 + this.x * p) << 0) / p; this.y = ((0.5 + this.y * p) << 0) / p; return this; }; /** * Returns a copy of this vector */ AbstractVector.prototype.clone = function () { return new this.ctor(this.x, this.y); }; return AbstractVector; }()); exports.AbstractVector = AbstractVector; //# sourceMappingURL=AbstractVector.js.map