Sierpinsky
in
Programming Questions
•
1 year ago
I know this is not the easiest way yo draw a sierpinsky triangle, but I want to get used to transformations and I think that this is a good way to make diferent fractals with about the same code.
I don´t know why this code doesn´t work and it says that there are too much recursion. I see that is very limited. I think this is the standard way to draw fractals
void setup(){
size(800, 800, P3D);
}
size(800, 800, P3D);
}
float side = 400;
int maxlevel=2;
void draw(){
float r=0.5;
float a=0;
float b=0;
float h=0;
float k=0;
translate (side,side);
rotate (PI);
triang(side);
subtriangles(1);
float r=0.5;
float a=0;
float b=0;
float h=0;
float k=0;
translate (side,side);
rotate (PI);
triang(side);
subtriangles(1);
noLoop();
}
}
void transform(float r, float a, float b, float h, float k){
translate(h, k);
shearX(a);
shearY(b);
scale(r);
}
void back(float r, float a, float b, float h, float k){
shearX(-a);
shearY(-b);
scale(1/r);
translate(-h, -k);
}
translate(h, k);
shearX(a);
shearY(b);
scale(r);
}
void back(float r, float a, float b, float h, float k){
shearX(-a);
shearY(-b);
scale(1/r);
translate(-h, -k);
}
void triang(float side){
line (0,0,side*cos(60*2*PI/360),side*sin(60*2*PI/360));
//println (400-400*cos(60/(2*PI)));
line (side*cos(60*2*PI/360), side*sin(60*2*PI/360), side , 0);
line (side , 0, 0, 0);
}
void subtriangles(int level){
println(level);
transform( 0.5, 0, 0,0,0);
triang(side);
if (level < maxlevel){
subtriangles(level+1);
}
back( 0.5, 0, 0,0,0);
transform(0.5,0,0,200,0);
triang(side);
if (level < maxlevel){
subtriangles(level+1);
}
back( 0.5,0,0,200,0);
transform(0.5,0,0,100,200*sin(60*2*PI/360));
subtriangles(level);
if (level < maxlevel){
subtriangles(level+1);
}
back(0.5,0,0,200,0);
}
line (0,0,side*cos(60*2*PI/360),side*sin(60*2*PI/360));
//println (400-400*cos(60/(2*PI)));
line (side*cos(60*2*PI/360), side*sin(60*2*PI/360), side , 0);
line (side , 0, 0, 0);
}
void subtriangles(int level){
println(level);
transform( 0.5, 0, 0,0,0);
triang(side);
if (level < maxlevel){
subtriangles(level+1);
}
back( 0.5, 0, 0,0,0);
transform(0.5,0,0,200,0);
triang(side);
if (level < maxlevel){
subtriangles(level+1);
}
back( 0.5,0,0,200,0);
transform(0.5,0,0,100,200*sin(60*2*PI/360));
subtriangles(level);
if (level < maxlevel){
subtriangles(level+1);
}
back(0.5,0,0,200,0);
}
1