UNPKG

ts-scikit

Version:

A scientific toolkit written in Typescript

185 lines 5.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Vector4 = void 0; const tuple4_1 = require("./tuple4"); /** * A vector with 4 components: x, y, and z. */ class Vector4 extends tuple4_1.Tuple4 { /** * Constructs a new vector with three components. * @param x the x-component. * @param y the y-component. * @param z the z-component. * @param w the w-component. */ constructor(x, y, z, w) { super(x, y, z, w); } /** * The length of this vector. * @returns the length of this vector. */ get length() { return Math.sqrt(this.lengthSquared); } /** * The length of this vector, squared. * @returns the length of this vector, squared. */ get lengthSquared() { return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; } /** * Computes the negation -u of this vector u. * @returns the negation. */ negate() { return new Vector4(-this.x, -this.y, -this.z, -this.w); } /** * Sets this vector to its negation. * @returns a reference to this vector. */ negateEquals() { this.x = -this.x; this.y = -this.y; this.z = -this.z; this.w = -this.w; return this; } /** * Computes the unit vector with the same direction as this vector. * @returns the unit vector. */ normalize() { const d = this.length; const s = (d > 0.0) ? 1.0 / d : 1.0; return new Vector4(this.x * s, this.y * s, this.z * s, this.w * s); } /** * Sets this vector to its unit vector. * @returns a reference to this vector. */ normalizeEquals() { const d = this.length; const s = (d > 0.0) ? 1.0 / d : 1.0; this.x *= s; this.y *= s; this.z *= s; this.w *= s; return this; } /** * Returns the vector sum u + v for this vector u * @param v the other vector. * @returns the vector sum u + v. */ plus(v) { return new Vector4(this.x + v.x, this.y + v.y, this.z + v.z, this.w + v.w); } /** * Adds a vector v to this vector u. * @param v the other vector. * @returns a reference to this vector, after adding vector v. */ plusEquals(v) { this.x += v.x; this.y += v.y; this.z += v.z; this.w += v.w; return this; } /** * Returns the vector difference u - v for this vector u. * @param v the other vector. * @returns the vector difference u - v. */ minus(v) { return new Vector4(this.x - v.x, this.y - v.y, this.z - v.z, this.w - v.w); } /* * Subtracts a vector v from this vector u. * @param v the other vector. * @returns a reference to this vector, after subtracting vector v. */ minusEquals(v) { this.x -= v.x; this.y -= v.y; this.z -= v.z; this.w -= v.w; return this; } /** * Returns the scaled vector s * u for this vector u. * @param s the scale factor. * @returns the scaled vector. */ times(s) { return new Vector4(this.x * s, this.y * s, this.z * s, this.w * s); } /** * Scales this vector. * @param s the scale factor. * @returns a reference to this vector, after scaling. */ timesEquals(s) { this.x *= s; this.y *= s; this.z *= s; this.w *= s; return this; } /** * Gets the angle between this and the provided vector. * @param v the vector. * @returns the angle (in radians) between the two vectors. */ angle(v) { let dls = this.dot(v) / (this.length * v.length); if (dls < -1) { dls = -1; } else if (dls > 1) { dls = 1; } return Math.acos(dls); } /** * Computes the dot product of this vector and the specified vector v. * @param v the vector v. * @returns the dot product. */ dot(v) { return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w; } /** * Returns the absolute of this vector. * @returns the absolute of this vector. */ abs() { return new Vector4(Math.abs(this.x), Math.abs(this.y), Math.abs(this.z), Math.abs(this.w)); } /** * Computes the squared distance between this and a specified vector. * @param v the vector. * @returns the distance between the vectors, squared. */ distanceSquared(v) { const dx = this.x - v.x; const dy = this.y - v.y; const dz = this.z - v.z; const dw = this.w - v.w; return dx * dx + dy * dy + dz * dz + dw * dw; } /** * Computes the distance between this and a specified vector. * @param v the vector. * @returns the distance between the vectors. */ distance(v) { return Math.sqrt(this.distanceSquared(v)); } } exports.Vector4 = Vector4; //# sourceMappingURL=vector4.js.map