@js-basics/vector

A 3D Vector lib including arithmetic operator overloading (+ - * / % **).

Usage no npm install needed!

<script type="module">
  import jsBasicsVector from 'https://cdn.skypack.dev/@js-basics/vector';
</script>

README

basics - vector

GitHub package version npm version license Renovate

OSX/Linux Build Status Windows Build status Dependencies Status DevDependencies Status

This library provides 3D Vector in js including arithmetic operator overloading (+ - * / % **).

Normally vector implementations in javascript handle arithmetic operation by methods aVec.multiply(bVec).substract(dVec). Other languages provide operator overloading, that coders can create Vector class which can handle operation similar to number handling aVec * bVec - dVec.

This library gives javascript coders a way to handle operators with a single statement () => aVec * bVec - dVec. The calculation can be combined with numbers () => aVec * bVec * 4 - dVec - 1.5. Vector objects can be create with number new Vector(5, 6, 7) or directly with assigned statement new Vector(() => 5 * 30 + 2).

Implementation details

// typical implementation of vector in js
const vec = aVec.multiply(bVec).multiply(4).substract(dVec).substract(1.5);

                    ⇓

// better readable, but much more complicated
const vec = new Vec(aVec.x * bVec.x * 4 - dVec.x - 1.5,
                    aVec.y * bVec.y * 4 - dVec.y - 1.5,
                    aVec.z * bVec.z * 4 - dVec.z - 1.5);

                    ⇓

// inspired by smart array handling
// first version of calling assigned function three times
const vec = oldCalc(  aVec, bVec, dVec,
                    ( aVec, bVec, dVec) => aVec * bVec * 4 - dVec - 1.5
                   );

                    ⇓

// final version with overwritten valueOf() method
const vec = calc(() => aVec * bVec * 4 - dVec - 1.5);

Javascript has this one peculiarity called valueOf() this method is designed for primitive handling (numbers and strings) when handling arithmetic operations. Every class can overwrite this method to give it special behavior. This Vector class calls the assigned statement three times for x, y and z. Comparable to trigger arithmetic operation manually for every axis.

Internally the valueOf() implementation returns x in first call, y in second call and z in last call, these results are put into an new Vector object and can be reused further.

Usage

load via hmtl

<script
  type="text/javascript"
  src="https://unpkg.com/@js-basics/vector/build/iife"
></script>
const { calc, vector, victor, point, ipoint } = basics.vector;

load via npm

$ npm i @js-basics/vector

import { calc, vector, victor, point, ipoint } from "@js-basics/vector";

working with Vector classes

create vector by numbers

code preview

create vector by calculating other vectors and number

code preview

calculate cross product

code preview

directly normalize the cross product

code preview

cross product handling works also with operator handling

code preview

normalize only with arithmetic

code preview

mutable 3D vector called Vector

code preview

immutable 3D vector called Victor

behaves exactly like Vector but code cant change its x, y and z axes.

code preview

mutable 2D vector called Point

code preview

immutable 2D vector called IPoint

behaves exactly like Point but code cant change its x and y axes.

code preview

creating vector inside calculation

works fine thanks to caching in factory function.

code preview

mixing 2D and 3D space

code preview

Not another vector lib

You are happy with your current vector library and definitely don’t want to change 1000 lines of code already written in your projects. Then you can only use the valueOf extension and the calc function.

working with Operator class

load via hmtl

<script
  type="text/javascript"
  src="https://unpkg.com/@js-basics/vector/build/iife/operator.js"
></script>
const { cachedValueOf, cachedFactory, operatorCalc } = basics.vector.operator;

load via npm

$ npm i @js-basics/vector

import {
  cachedValueOf,
  cachedFactory,
  operatorCalc
} from "@js-basics/vector/operator";

override valueOf in own class

code preview

write own factory function

code preview

Predefined adapters

existing operator overloading for game engines or other vector libraries