Skip to content

Instantly share code, notes, and snippets.

@MrTelanie
Created February 23, 2019 12:50
Show Gist options
  • Select an option

  • Save MrTelanie/a5828d06f4bdbcf3007f2aea89af9556 to your computer and use it in GitHub Desktop.

Select an option

Save MrTelanie/a5828d06f4bdbcf3007f2aea89af9556 to your computer and use it in GitHub Desktop.
Operator Handler for Arrays
proposal or How I could use it:
// hijack valeOf & toString of Array.prototype
// create vector by numbers
const pos = [5, 6, 7];
const dir = [1, 0, 0];
console.log(debug`pos:${pos}, dir: ${dir}`);
// pos: { x: 5, y: 6, z: 7 } dir: { x: 1, y: 0, z: 0 }
// calc two Vectors and numbers with operator
const offsetA = calc(() => dir * 30 + pos);
console.log(debug`offsetA: ${offsetA}`);
// offsetA: { x: 35, y: 6, z: 7 }
// calc two Vectors and numbers with operator
const offsetB = calc(() => dir * 20 + pos);
console.log(debug`offsetB: ${offsetB}`);
// offsetB: { x: 25, y: 6, z: 7 }
// compare length
let way = offsetA;
if (way > 1) {
way = normalize(way);
}
console.log(debug`way: ${way}`);
// way: { x: 0.967, y: 0.1658, z: 0.1934 }
// calculate cross product
const dir1 = [0, 1, 0];
const dir2 = [-1, 0, 1];
const cross = cross(dir1, dir2);
console.log(debug`cross: ${cross}`);
// cross: { x: 1, y: 0, z: 1 }
// directly normalize the cross product
const crossNorm = crossNormalize(dir1, dir2);
console.log(debug`crossNorm: ${crossNorm}`);
// crossNorm: { x: 0.7071, y: 0, z: 0.7071 }
// cross product handling works also with operator handling
const crossNormCalc = calc(() => crossNormalize(dir1, dir2) * 50);
console.log(debug`crossNormCalc: ${crossNormCalc}`);
// crossNormCalc: { x: 35.36, y: 0, z: 35.36 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment