Hi!
I’m continuing with my efforts to replicate early works of computer art.
I made this nice sketch to replicate Manfred Mohr’s Pascal triangle:
http://p5js.sketchpad.cc/NbccVM8Nvk
And then I made an animated version, saving the parameters of each cube in order to make it on the draw() function:
https://editor.p5js.org/jgmy/sketches/-045EqDcU
Well, it seems that the code works different on setup() and draw().
I will copy below the relevant part, adding some comments.
draw () function on working version 1:
/* r is width/3, it is, 25 since I'm using a 300x300 WebGL canvas */
pg = createGraphics(r*3, r*3, WEBGL);
pg.ortho(-pg.width / 2, pg.width / 2, -pg.height / 2, pg.height / 2, 0, 500);
pg.rotateX(PI / 4);
pg.rotateY(PI / 3);
pg.stroke(255);
pg.strokeWeight(r/10);
pg.background(0);
for(let piso=0;piso<Arista.length;piso++){
for (let columna=0; columna<=piso; columna++){
pg.background(0);
for (let paso=0;paso<(Arista.length-piso);paso++){
f=paso+columna;
pg.line(
/* Arista[] is a shared array with x and y positions for the edges of a cube */
Arista[f].x1, Arista[f].y1,Arista[f].z1,
Arista[f].x2,Arista[f].y2,Arista[f].z2
);
image(pg,(columna-0.5*piso)*3*r, (piso-6)*3*r);
}
}
End of setup() and complete draw() on (failing) version 2:
setup() {
/* ... */
for(let piso=0;piso<Arista.length;piso++){
for (let columna=0; columna<=piso; columna++){
Cube.unshift({
"x":(columna-0.5*piso)*3*r,
"y":(piso-6)*3*r,
"piso":piso,
"columna":columna,
});
}
}
}
function draw() {
/*
I have checked all the shared variables to make sure
"r" lands here with a working value.
*/
numCube=int(frameCount/10) % (Cube.length);
piso=Cube[numCube].piso;
columna=Cube[numCube].columna;
xc=Cube[numCube].x;
yc=Cube[numCube].y;
console.log(numCube);
let pg2 = createGraphics(r*3, r*3, WEBGL);
pg2.ortho(-pg2.width / 2, pg2.width / 2,
-pg2.height / 2,pg2.height / 2, 0,500);
pg2.rotateX(PI / 4);
pg2.rotateY(PI / 3);
pg2.stroke(255);
pg2.strokeWeight(r/10);
pg2.background(0);
for (let paso=0;paso<(Arista.length-piso);paso++){
f=paso+columna;
pg2.line(
Arista[f].x1, Arista[f].y1,Arista[f].z1,
Arista[f].x2,Arista[f].y2,Arista[f].z2
);
}
image(pg2,xc, yc);
/* If you run the next instruction, you'll see image fills the entire canvas,
while its dimensions are exactly the same than in version 1 (25*25 pixels).
*/
// image(pg2,0,0);
}
Result of version 1 (drawing on setup()):
Cube[2] of version 2 (drawing on draw()) if you uncomment image(pg2,0,0):
Notice the cube edge should be as small as in the previous version, but it is bigger.
It seems that pgraphics pasted at setup() produces 2D images, while pgraphics pasted at draw() are pasted on a 3D space.
Also, from error messages I get, it seems that minimal value of clipping at setup() seems to be zero, but clipping at draw() seems to have a minimal value of .01.
I understand I could use translate() and scale(), but I want to understand how things work first.