UNPKG

nowjs-core

Version:

NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence

205 lines (204 loc) 5.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Point4D_1 = require("./Point4D"); class Vector4D extends Point4D_1.Point4D { constructor(x, y, z, w) { super(x, y, z, w); } static negative(a, b) { b.X = -a.X; b.Y = -a.Y; b.Z = -a.Z; b.W = -a.W; return b; } static add(a, b, c) { c = c || new Vector4D(); if (b instanceof Vector4D) { c.X = a.X + b.X; c.Y = a.Y + b.Y; c.Z = a.Z + b.Z; c.W = a.W + b.W; } else { c.X = a.X + b; c.Y = a.Y + b; c.Z = a.Z + b; c.W = a.W + b; } return c; } static subtract(a, b, c) { c = c || new Vector4D(); if (b instanceof Vector4D) { c.X = a.X - b.X; c.Y = a.Y - b.Y; c.Z = a.Z - b.Z; c.W = a.W - b.W; } else { c.X = a.X - b; c.Y = a.Y - b; c.Z = a.Z - b; c.W = a.W - b; } return c; } static multiply(a, b, c) { c = c || new Vector4D(); if (b instanceof Vector4D) { c.X = a.X * b.X; c.Y = a.Y * b.Y; c.Z = a.Z * b.Z; c.W = a.W * b.W; } else { c.X = a.X * b; c.Y = a.Y * b; c.Z = a.Z * b; c.W = a.W * b; } return c; } static divide(a, b, c) { c = c || new Vector4D(); if (b instanceof Vector4D) { c.X = a.X / b.X; c.Y = a.Y / b.Y; c.Z = a.Z / b.Z; c.W = a.W / b.W; } else { c.X = a.X / b; c.Y = a.Y / b; c.Z = a.Z / b; c.W = a.W / b; } return c; } static cross(a, b, c) { c = c || new Vector4D(); c.X = a.Y * b.Z - a.Z * b.Y; c.Y = a.Z * b.X - a.X * b.Z; c.Z = a.X * b.Y - a.Y * b.X; c.W = a.W * b.W - a.W * b.X; return c; } static unit(a, b) { const length = a.length(); b.X = a.X / length; b.Y = a.Y / length; b.Z = a.Z / length; b.W = a.W / length; return b; } static fromBearing(angle, length) { return new Vector4D(length * Math.cos(angle), length * Math.sin(angle)); } static fromAngles(theta, phi) { return new Vector4D(Math.cos(theta) * Math.cos(phi), Math.sin(phi), Math.sin(theta) * Math.cos(phi)); } static randomDirection() { return Vector4D.fromAngles(Math.random() * Math.PI * 2, Math.asin(Math.random() * 2 - 1)); } static min(a, b) { return new Vector4D(Math.min(a.X, b.X), Math.min(a.Y, b.Y), Math.min(a.Z, b.Z)); } static max(a, b) { return new Vector4D(Math.max(a.X, b.X), Math.max(a.Y, b.Y), Math.max(a.Z, b.Z)); } static lerp(a, b, fraction) { return b .subtract(a) .multiply(fraction) .add(a); } static fromArray(a) { return new Vector4D(a[0], a[1], a[2]); } static angleBetween(a, b) { return a.angleTo(b); } negative() { return new Vector4D(-this.X, -this.Y, -this.Z); } add(v) { if (v instanceof Vector4D) return new Vector4D(this.X + v.X, this.Y + v.Y, this.Z + v.Z); else return new Vector4D(this.X + v, this.Y + v, this.Z + v); } subtract(v) { if (v instanceof Vector4D) return new Vector4D(this.X - v.X, this.Y - v.Y, this.Z - v.Z); else return new Vector4D(this.X - v, this.Y - v, this.Z - v); } multiply(v) { if (v instanceof Vector4D) return new Vector4D(this.X * v.X, this.Y * v.Y, this.Z * v.Z); else return new Vector4D(this.X * v, this.Y * v, this.Z * v); } divide(v) { if (v instanceof Vector4D) return new Vector4D(this.X / v.X, this.Y / v.Y, this.Z / v.Z); else return new Vector4D(this.X / v, this.Y / v, this.Z / v); } equals(v) { return this.X === v.X && this.Y === v.Y && this.Z === v.Z; } dot(v) { return this.X * v.X + this.Y * v.Y + this.Z * v.Z + this.W * v.W; } cross(v) { return new Vector4D(this.Y * v.Z - this.Z * v.Y, this.Z * v.X - this.X * v.Z, this.X * v.Y - this.Y * v.X); } magnitude() { return this.length(); } length() { return Math.sqrt(this.dot(this)); } unit() { return this.divide(this.length()); } min() { return Math.min(Math.min(Math.min(this.X, this.Y), this.Z), this.W); } max() { return Math.max(Math.max(Math.max(this.X, this.Y), this.Z), this.W); } toAngles() { return { theta: Math.atan2(this.Z, this.X), phi: Math.asin(this.Y / this.length()), }; } angleTo(a) { return Math.acos(this.dot(a) / (this.length() * a.length())); } areaOfTriangle(a) { const c = this.cross(a).magnitude(); return 0.5 * c; } volumeOfVectors(a, b) { const v = Math.abs(this.cross(b).dot(a)); return (1 / 6) * v; } toArray(n) { return [this.X, this.Y, this.Z, this.W].slice(0, n || 4); } clone() { return new Vector4D(this.X, this.Y, this.Z, this.W); } init(x, y, z, w) { this.X = x; this.Y = y; this.Z = z; this.W = w; return this; } } exports.Vector4D = Vector4D;