RosettaCodeData/Task/Sierpinski-triangle-Graphical/Processing/sierpinski-triangle-graphic...

61 lines
1.2 KiB
Plaintext

import peasy.*;
int depth = 6; // recursion depth
int dWidth = 600;
int dHeight = 600;
color pyramidColor = color( 0 );
color bgColor = color( 255 );
// 3D Sierpinski tetrahedron vertices
PVector [] coord = {
new PVector( 0, 0, 0),
new PVector( 300, 0, 0),
new PVector( 150, 0, -260),
new PVector( 150, -245, -86.6)
};
int verts = coord.length;
float boxSize = 600/pow(3, depth);
// "random" start point (mid point)
PVector startPoint = new PVector(150, 183.7, 173.2);
PeasyCam cam;
void settings()
{
size(dWidth, dHeight, P3D);
}
void setup()
{
cam = new PeasyCam(this, startPoint.x, startPoint.y, startPoint.z, 400);
cam.setMaximumDistance(3000);
fill(pyramidColor);
stroke(pyramidColor);
}
void draw()
{
background(bgColor);
sierpinski(startPoint, depth);
}
void sierpinski(PVector currentPoint, int currentDepth)
{
if (currentDepth == 0) {
pushMatrix();
translate(currentPoint.x, 245+currentPoint.y, 260+currentPoint.z);
box(boxSize);
popMatrix();
return;
}
for (int v=0; v<verts; v++) {
sierpinski(new PVector(
(currentPoint.x+coord[v].x)/2,
(currentPoint.y+coord[v].y)/2,
(currentPoint.z+coord[v].z)/2),
currentDepth-1);
}
}