enkimute / ganja.js

:triangular_ruler: Javascript Geometric Algebra Generator for Javascript, c++, c#, rust, python. (with operator overloading and algebraic literals) -
MIT License
1.52k stars 106 forks source link

Using closure variables in Algebra inline function #111

Open kungfooman opened 3 years ago

kungfooman commented 3 years ago

Currently this is not possible:

image

And it requires window/global hacks:

var externalData = [1, 2, 3];
window.externalData = externalData;
// Create a Clifford Algebra with 2,0,1 metric. 
Algebra(2,0,1,()=>{
  console.log("externalData", externalData)
});
jahbini commented 3 years ago

I found an example of passing in quantities in the source for : Ganja's examples/example_game_wedge.html Not the prettiest, but it's better than window hacks. Maybe. There are a few other examples of the use of inline in the Ganja examples directory.

Here is my use of passing Observable viewX (sample from https://observablehq.com/@liuyao12/geometric-algebra)

`{var A301=Algebra(3,0,1),vx=viewX, valuate=A301.inline(
  (vx)=>{
    var gridX = Array.apply([],{length:21}).map((x,i)=>[1e123+2e012+1e013+(i-10)*.2e023,1e123-2e012+1e013+(i-10)*.2e023]);
    var gridY = Array.apply([],{length:21}).map((x,i)=>[1e123+2e023+1e013+(i-10)*.2e012,1e123-2e023+1e013+(i-10)*.2e012]);
    var orig = 1e123-1e013;
    var p = 1e123+1e013+.65e023-.9e012;
    var p2 = 1e123+1e013-1.35e023+.8e012;
    var p3 = 1e123+1e013+1.35e023+1.1e012;
    var p4 = p.slice();
    var camera=1+0e0, t=0;
    debugger;
    console.log('line : ',(orig&p2&p3)^(p&p2&p3));
    var el=this.graph(()=>{
      t = vx;
      camera.set(Math.cos(t)+Math.sin(t)*1e13);
      return [orig,"origin in 3D",
             gridX,gridY,
             0x4444cc, p,"point in 2D",
             orig&p,"vector in 3D",
             0xffcccc,[orig,p2,p3],0xCC4444,"triangle in 3D",p2&p3,"line in 2D"]
      },
      {gl:true,grid:true, animate:false,camera});  //
    el.style.width='100%', el.style.height='1024px'; return el;
    }
  );
 return valuate(viewX);
}`