Sierpinski triangle/Graphical: Difference between revisions

Content added Content deleted
(Added Wren)
Line 1,632: Line 1,632:
Recursive Sierpinski triangle
Recursive Sierpinski triangle
<lang Processing>
<lang Processing>
PVector [] coord = {new PVector(0, 0), new PVector(150, 300), new PVector(300, 0)};
int depth = 8; // recursion depth
color c = color(192);
PVector startPoint;

// Sierpinski triangle vertices
PVector [] coord = {
new PVector( 0, 0),
new PVector(150, 300),
new PVector(300, 0)
};


void setup()
void setup()
Line 1,647: Line 1,638:
size(400,400);
size(400,400);
background(32);
background(32);
sierpinski(new PVector(150,150), 8);
// random start point
startPoint = new PVector(150, 150);

sierpinski(startPoint, depth);
noLoop();
noLoop();
}
}


void sierpinski(PVector cPoint, int cDepth)

void draw(){}


void sierpinski(PVector currentPoint, int currentDepth)
{
{
if (currentDepth == 0) {
if (cDepth == 0) {
set(50+int(currentPoint.x), (height-50)-int(currentPoint.y), c);
set(50+int(cPoint.x), (height-50)-int(cPoint.y), color(192));
return;
return;
}
}
for (int v=0; v<3; v++) {
PVector temp = currentPoint.get();
for (PVector sVertice : coord) {
sierpinski(new PVector((cPoint.x+coord[v].x)/2, (cPoint.y+coord[v].y)/2), cDepth-1);
currentPoint.set((currentPoint.x+sVertice.x)/2, (currentPoint.y+sVertice.y)/2);
sierpinski(currentPoint, currentDepth-1);
currentPoint = temp.get();
}
}
}
}