UNPKG

cerceis-lib

Version:

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

264 lines (262 loc) 7.54 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/vector/index.ts var vector_exports = {}; __export(vector_exports, { FromVector: () => FromVector, createVector: () => createVector }); module.exports = __toCommonJS(vector_exports); // 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 }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FromVector, createVector }); //# sourceMappingURL=index.js.map