UNPKG

cerceis-lib

Version:

Contains list of quality of life functions that is written in TypeScript and es6

236 lines 6.46 kB
// src/fromvector/fromVector.ts var createVector = (x, y, z = 0) => { const vectorObject = { isVectorObject: true, x, y, z, /** * @param x { VectorObject | Number[] | number } * @returns */ add(x2, y2 = 0, z2 = 0) { if (x2 instanceof Array) { this.x += x2[0] || 0; this.y += x2[1] || 0; this.z += x2[2] || 0; return this; } if (x2.isVectorObject) { this.x += x2.x || 0; this.y += x2.y || 0; this.z += x2.z || 0; return this; } this.x += x2 || 0; this.y += y2 || 0; this.z += z2 || 0; return this; }, limit(max) { const mSq = this.magSq(); if (mSq > max * max) { this.div(Math.sqrt(mSq)).mult(max); } return this; }, /** * * @param x { VectorObject | Number[] | number } * @param y { number } * @param z { number } * @returns */ div(x2, y2 = 0, z2 = 0) { if (x2.isVectorObject) { if (Number.isFinite(x2.x) && Number.isFinite(x2.y) && Number.isFinite(x2.z) && typeof x2.x === "number" && typeof x2.y === "number" && typeof x2.z === "number") { this.x /= x2.x; this.y /= x2.y; this.z /= x2.z; } return this; } if (x2 instanceof Array) { if (x2.every((element) => Number.isFinite(element)) && x2.every((element) => typeof element === "number")) { if (x2.some((element) => element === 0)) { console.warn("Divide by 0"); return this; } if (x2.length === 1) { this.x /= x2[0]; this.y /= x2[0]; this.z /= x2[0]; } else if (x2.length === 2) { this.x /= x2[0]; this.y /= x2[1]; } else if (x2.length === 3) { this.x /= x2[0]; this.y /= x2[1]; this.z /= x2[2]; } } else { console.warn( "x contains components that are either undefined or not finite numbers" ); } return this; } const vectorComponents = [...arguments]; if (vectorComponents.every((element) => Number.isFinite(element)) && vectorComponents.every((element) => typeof element === "number")) { if (vectorComponents.some((element) => element === 0)) { console.warn("Divide by 0"); return this; } if (arguments.length === 1) { this.x /= x2; this.y /= x2; this.z /= x2; } if (arguments.length === 2) { this.x /= x2; this.y /= y2; } if (arguments.length === 3) { this.x /= x2; this.y /= y2; this.z /= z2; } } else { console.warn( "x, y, or z arguments are either undefined or not a finite number" ); } return this; }, /** * * @param x { VectorObject | Number[] | number } * @param y { number } * @param z { number } * @returns */ mult(x2, y2 = 0, z2 = 0) { if (x2.isVectorObject) { if (Number.isFinite(x2.x) && Number.isFinite(x2.y) && Number.isFinite(x2.z) && typeof x2.x === "number" && typeof x2.y === "number" && typeof x2.z === "number") { this.x *= x2.x; this.y *= x2.y; this.z *= x2.z; } else { console.warn( "x contains components that are either undefined or not finite numbers" ); } return this; } if (x2 instanceof Array) { if (x2.every((element) => Number.isFinite(element)) && x2.every((element) => typeof element === "number")) { if (x2.length === 1) { this.x *= x2[0]; this.y *= x2[0]; this.z *= x2[0]; } else if (x2.length === 2) { this.x *= x2[0]; this.y *= x2[1]; } else if (x2.length === 3) { this.x *= x2[0]; this.y *= x2[1]; this.z *= x2[2]; } } else { console.warn( "x contains elements that are either undefined or not finite numbers" ); } return this; } const vectorComponents = [...arguments]; if (vectorComponents.every((element) => Number.isFinite(element)) && vectorComponents.every((element) => typeof element === "number")) { if (arguments.length === 1) { this.x *= x2; this.y *= x2; this.z *= x2; } if (arguments.length === 2) { this.x *= x2; this.y *= y2; } if (arguments.length === 3) { this.x *= x2; this.y *= y2; this.z *= z2; } } else { console.warn( "x, y, or z arguments are either undefined or not a finite number" ); } return this; }, heading() { const h = Math.atan2(this.y, this.x); return h; }, /* * @method sub * @param {VectorObject | Number[]} value the vector to subtract * @chainable */ sub(x2, y2 = 0, z2 = 0) { if (x2.isVectorObject) { this.x -= x2.x || 0; this.y -= x2.y || 0; this.z -= x2.z || 0; return this; } if (x2 instanceof Array) { this.x -= x2[0] || 0; this.y -= x2[1] || 0; this.z -= x2[2] || 0; return this; } this.x -= x2 || 0; this.y -= y2 || 0; this.z -= z2 || 0; return this; }, setMag(n) { return this.normalize().mult(n); }, magSq() { const x2 = this.x; const y2 = this.y; const z2 = this.z; return x2 * x2 + y2 * y2 + z2 * z2; }, mag() { return Math.sqrt(this.magSq()); }, normalize() { const len = this.mag(); if (len !== 0) this.mult(1 / len); return this; }, copy() { return createVector(this.x, this.y, this.z); }, dist(x2, y2 = 0) { if (x2.isVectorObject) { return Math.hypot(x2.x - this.x, x2.y - this.y); } return Math.hypot(x2 - this.x, y2 - this.y); }, toVector2() { return [this.x, this.y]; }, toVector3() { return [this.x, this.y, this.z]; } }; return vectorObject; }; var FromVector = { create: createVector }; export { FromVector, createVector }; //# sourceMappingURL=index.mjs.map