We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi. Im trying to make sketches to design fractals. There are several ways to draw one, but all of them are based on transformations. I´ve made one to generate sierspinsky triangle, and it works. Ive change the parameters to make a Barnsley Fern, and the result has nothing to do with it. I´ve taken the parameters from this page. home.aone.net.au/~byzantium/ferns/fractal.html . Im not drawing the central line.
This is the code for sierpinsky
class Forma{
float[] x = new float[4];
float[] y = new float [4];
Forma(float x0, float x1, float x2, float x3, float y0, float y1, float y2, float y3){
x[0]=x0;x[1]=x1;x[2]=x2;x[3]=x3; y[0]=y0;y[1]=y1;y[2]=y2;y[3]=y3;
}
void render(){
quad(x[0]*escala,y[0]*escala,x[1]*escala,y[1]*escala,x[3]*escala,y[3]*escala,x[2]*escala,y[2]*escala);
}
}
int n = 3; //número de transformaciones
ArrayList formas= new ArrayList();
int iterando = 0;
int escala =300;
void setup() {
size(500, 500, P2D);
background(250);
formas.add ( new Forma(0.0, 1.0,0.0, 1.0, 0.0,0.0,1.0,1.0));
noLoop();
}
int iterations = 4;
float x=0.5;
float y=0.5;
color col = color(100, 50,255);
int maxiterations = 1000000;
void draw() {
background(255);
fill(255,100,100);
for(int i = 0; i<iterations;i++){
itera();
background(255);
}
render();
/**for (int i = 0; i < maxiterations; i++) {
transform();
}
save ("sierpinski.jpg");
*/
}
void itera(){
//println("iterando");
float[] a = {0.5, 0.5, 0.5};
float[] b = { 0, 0, 0};
float[] c = { 0, 0,0};
float[] d = { 0.5, 0.5,0.5};
float[] e = { 0, 0.5, 0.25};
float[] f = { 0,0,0.5};
float[] x = new float[4];
float[] y = new float [4];
int total=formas.size();
println("total="+total);
for(int i=0;i<total;i++){
Forma form = (Forma) formas.get(i);
//println("forma cogida");
for(int j=0;j<4;j++){
//println("x="+ form.x[j]);
//println("y="+ form.y[j]);
}
for (int k = 0; k < n; k++){
//println("puntos");
for (int j = 0; j < 4; j++){
x[j] = form.x[0]+a[k]*(form.x[j]-form.x[0]) + b[k] * (form.y[j]-form.y[0]) + e[k]*(form.x[1]-form.x[0]);;
//println("x="+x[j]);
y[j] = form.y[0]+c[k]*(form.x[j]-form.x[0]) + d[k] * (form.y[j]-form.y[0]) + f[k]*(form.y[3]-form.y[0]);
//println("y="+y[j]);
}
formas.add ( new Forma(x[0],x[1],x[2],x[3],y[0],y[1],y[2],y[3]));
println("creating "+x[0]+" "+x[1]+" "+x[2]+" "+x[3]+" "+y[0]+" "+y[1]+" "+y[2]+" "+y[3]);
//quad(x[0]*escala,y[0]*escala,x[1]*escala,y[1]*escala,x[3]*escala,y[3]*escala,x[2]*escala,y[2]*escala);
}
}
for(int i=total-1;i>=0;i--){
Forma form = (Forma) formas.get(i);
println("erasing "+i);
muestra(form);
formas.remove(i);
}
/**for(int i=0;i<formas.size();i++){
Forma form = (Forma) formas.get(i);
println("forma"+i);
for(int j=0;j<4;j++){
println("x="+ form.x[j]);
println("y="+ form.y[j]);
}
}*/
}
void render(){
for(int i=0;i<formas.size();i++){
Forma f = (Forma) formas.get(i);
f.render();
}
}
void muestra(Forma form){
for(int j=0;j<4;j++){
println("x="+ form.x[j]);
println("y="+ form.y[j]);
}
}
And this is the code for Barnsley
class Forma{
float[] x = new float[4];
float[] y = new float [4];
Forma(float x0, float x1, float x2, float x3, float y0, float y1, float y2, float y3){
x[0]=x0;x[1]=x1;x[2]=x2;x[3]=x3; y[0]=y0;y[1]=y1;y[2]=y2;y[3]=y3;
}
void render(){
quad(x[0]*escala+despl,y[0]*escala ,x[1]*escala + despl,y[1]*escala ,x[3]*escala + despl,y[3]*escala ,x[2]*escala + despl,y[2]*escala);
}
}
int n = 3; //número de transformaciones
ArrayList formas= new ArrayList();
int iterando = 0;
int escala =200;
int despl =300;
void setup() {
size(500, 500, P2D);
background(250);
formas.add ( new Forma(0.0, 1.0,0.0, 1.0, 0.0,0.0,1.0,1.0));
noLoop();
}
int iterations = 8;
float x=0.5;
float y=0.5;
color col = color(100, 50,255);
int maxiterations = 1000000;
void draw() {
background(255);
fill(255,100,100);
for(int i = 0; i<iterations;i++){
itera();
background(255);
}
render();
/**for (int i = 0; i < maxiterations; i++) {
transform();
}
save ("sierpinski.jpg");
*/
}
void itera(){
//println("iterando");
float[] a = {0.85, 0.2, -0.15};
float[] b = { 0.04, -0.26, 0.28};
float[] c = { -0.04, 0.23,0.26};
float[] d = { 0.85, 0.22,0.24};
float[] e = { 0, 0, 0};
float[] f = { 1.6,1.6,0.44};
float[] x = new float[4];
float[] y = new float [4];
int total=formas.size();
println("total="+total);
for(int i=0;i<total;i++){
Forma form = (Forma) formas.get(i);
//println("forma cogida");
for(int j=0;j<4;j++){
//println("x="+ form.x[j]);
//println("y="+ form.y[j]);
}
for (int k = 0; k < n; k++){
//println("puntos");
for (int j = 0; j < 4; j++){
x[j] = form.x[0]+a[k]*(form.x[j]-form.x[0]) + b[k] * (form.y[j]-form.y[0]) + e[k]*(form.x[1]-form.x[0]);;
//println("x="+x[j]);
y[j] = form.y[0]+c[k]*(form.x[j]-form.x[0]) + d[k] * (form.y[j]-form.y[0]) + f[k]*(form.y[3]-form.y[0]);
//println("y="+y[j]);
}
formas.add ( new Forma(x[0],x[1],x[2],x[3],y[0],y[1],y[2],y[3]));
println("creating "+x[0]+" "+x[1]+" "+x[2]+" "+x[3]+" "+y[0]+" "+y[1]+" "+y[2]+" "+y[3]);
//quad(x[0]*escala,y[0]*escala,x[1]*escala,y[1]*escala,x[3]*escala,y[3]*escala,x[2]*escala,y[2]*escala);
}
}
for(int i=total-1;i>=0;i--){
Forma form = (Forma) formas.get(i);
println("erasing "+i);
muestra(form);
formas.remove(i);
}
/**for(int i=0;i<formas.size();i++){
Forma form = (Forma) formas.get(i);
println("forma"+i);
for(int j=0;j<4;j++){
println("x="+ form.x[j]);
println("y="+ form.y[j]);
}
}*/
}
void render(){
for(int i=0;i<formas.size();i++){
Forma f = (Forma) formas.get(i);
f.render();
}
}
void muestra(Forma form){
for(int j=0;j<4;j++){
println("x="+ form.x[j]);
println("y="+ form.y[j]);
}
}
Answers
(please avoid /* */ comments in posted code, the forum often ignores the end marker. oddly, though, /** */ comments with that extra leading * look ok)
I inserted
float f1 = 1.6; // factor
as factor and used it to calculate the formula in several places - just to make it bigger.looks still weird though
I also moved the global vars so that they are in one place before
setup