Package Exports
- @js-basics/vector
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@js-basics/vector) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
basics - vector
This libary provides 3D Vector in js including support for+ - * /
Normally vector implementations in javascript handle arithmetic operation by functionsaVec.multiply(bVec).substract(dVec). aVec * bVec - dVec.
This libary gives javascript coders a way to handle operators with a single statement() => aVec * bVec - dVec. () => aVec * bVec * 4 - dVec - 1.5. new Vector(5, 6, 7) 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() function
const vec = calc(() => aVec * bVec * 4 - dVec - 1.5);Javascript has this one peculiarity called valueOf() this function is designed for primitive handling (numbers and strings) when handling arithmetic operations.
Every class can overwrite this function 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.
working with Vector class
create vector by numbers
create vector by calculating other vectors and number
calc two Vectors and numbers with operator
compare lengths
calculate cross product
directly normalize the cross product
cross product handling works also with operator handling
immutable vector called Victor
behaves exactly like Vector but code cant change its x, y and z axes.