JSPM

  • Created
  • Published
  • Downloads 341
  • Score
    100M100P100Q69083F
  • License MIT

A vector lib with support for (+ - * /) operator handling

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

GitHub package version npm version license Greenkeeper badge

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

This libary provides 3D Vector in js including support for + - * / operator handling.

Normally vector implementations in javascript handle arithmetic operation by functions 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 libary 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

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.

const x = aVec.x * bVec.x * 4 - dVec.x - 1.5;
const y = aVec.y * bVec.y * 4 - dVec.y - 1.5;
const z = aVec.z * bVec.z * 4 - dVec.z - 1.5;

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

const pos = new Vector(5, 6, 7);
const dir = new Vector(1, 0, 0);

console.log("pos:", pos, " dir:", dir);

pos: { x: 5, y: 6, z: 7 } dir: { x: 1, y: 0, z: 0 }

create vector by calculating other vectors and number

const offset = new Vector(() => dir * 30 + pos);

console.log("offset:", offset);

offset: { x: 35, y: 6, z: 7 }

calc two Vectors and numbers with operator

const offsetB = calc(() => dir * 20 + pos);
console.log("offsetB:", offsetB);

offsetB: { x: 25, y: 6, z: 7 }

compare lengths

let way = offset;
if (way > 1) {
  way = way.normalize();
}
console.log("way:", way);

way: { x: 0.96, y: 0.16, z: 0.19 }

calculate cross product

const dir1 = new Vector(0, 1, 0);
const dir2 = new Vector(-1, 0, 1);
const cross = dir1.cross(dir2);

console.log("cross:", cross);

cross: { x: 1, y: 0, z: 1 }

directly normalize the cross product

const crossNorm = dir1.crossNormalize(dir2);

console.log("crossNorm:", crossNorm);

crossNorm: { x: 0.71, y: 0, z: 0.71 }

cross product handling works also with operator handling

const crossNormCalc = new Vector(() => dir1.crossNormalize(dir2) * 50);

console.log("crossNormCalc:", crossNormCalc);

crossNormCalc: { x: 35.36, y: 0, z: 35.36 }

immutable vector called Victor

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

const vec = new Victor(5, 6, 7);

try {
  vec.x = 27;
} catch (error) {
  console.log("error:", error);
}

error: set x() not implemented

running example

code preview