Last active
March 20, 2022 12:25
-
-
Save ToJans/8435fc78665050e45a5cf84c3f396116 to your computer and use it in GitHub Desktop.
Ganja.js extrude cheat sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.d = 2; | |
Algebra(d,0,1,()=>{ | |
var point = (x,y)=>!(1e0 + x*1e1 + y*1e2), | |
// return functions instead of values, so they update when you drag the points | |
linesFromPoints = ps => ps.map((p,i,a) => ()=>p & (a[(i+1)%a.length]||a[0])), | |
pointsFromLines = e => e.map((ev,i,a) => ()=> ev * (a[(i+1)%a.length]||a[0])), | |
vectorToTranslationRotor = (v, amount)=> ()=> 1+amount*v.Normalized*1e0, | |
applyRotor=(obj,rotor)=> ()=>rotor>>>obj; | |
var graphF = (arr,labelPrefix) => arr.map((x,i)=>[x,labelPrefix+(i+1)]).flat(); | |
// Extrude edges by given amount - by enkimute | |
var extrude = (poly,amount=0.1)=> | |
poly.map((x,i,a)=>x&(a[i+1]||a[0])) // to edges | |
.map(x=>window.d==2?x : (plane|x)) // in 3D we want planes | |
.map((x,i)=>(1+amount*x.Normalized*1e0)>>>x) // extrude edges | |
.map((x,i,a)=>x^(a[i+1]||a[0])) // intersect edges. | |
.map(x=>window.d==2?x: x^plane); // in 3D intersect with poly plane again. | |
const amount = 0.2; | |
var a = point(-0.5,0.5), | |
b = point(0.5,0.5), | |
c = point(0,1), | |
d = point(-0.5,1), | |
points = [a,b,c,d], | |
edges = linesFromPoints(points), | |
rotors = edges.map(e=>vectorToTranslationRotor(e,0.2)), | |
outsetEdges = edges.map((E,i)=>()=>applyRotor(E,rotors[i])), | |
newPoints = pointsFromLines(outsetEdges) | |
document.body.appendChild(this.graph([ | |
"Outset experiment with PGA2D - twitter: @ToJans - Drag the blue points", | |
newPoints, | |
0xaaaaaa, | |
points, | |
0x0000088, | |
...graphF(points,"P"), | |
...graphF(edges,"E"), | |
0x0880000, | |
...graphF(rotors,"R"), | |
0x0008800, | |
...graphF(outsetEdges,"O"), | |
...graphF(newPoints,"NP"), | |
1e0,"1E0" | |
],{lineWidth:3, grid:true, labels:true, pointradius:4})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment