Hey folks, i want different colors at every branch of my tree. But why does it flicker all the time?

I want to set one random color, for every branch. But why the hell ist there always a new color for every branch. want to stop it. Hope you can help me :) see the random action at void(linie)

String Axiom="FL";                      //"FX"
String ER="F<F-[F^>>-F+F]+[>&+F-FL]";   //"FF-[F^-F+FL]+[>&+F-FL]"
String ER2="F&F+<[F><[>+F]]+&[F<^&--FL]";  //"FF+[F&>+FL]+[F<^-FL]"
String NeuER="";   
String Zwsp=""; 


void setup() {
  size(1200, 1000, P3D);
  smooth(); 
  strokeWeight(0.8);
  frameRate(300);
  for (int a=0; a<=5; a++) {
    if (a==0) {
      NeuER=NeuER+Axiom; 
    } else {
      for (int b=0; b<NeuER.length(); b++) {
        if (NeuER.charAt(b)=='F') {
          Zwsp=Zwsp + ER;
        } else if (NeuER.charAt(b)=='X') {
          Zwsp=Zwsp + ER2;
        } else {
          Zwsp=Zwsp + NeuER.charAt(b);
        }     
      }
      NeuER=Zwsp;   
      Zwsp="";
    }
  }
  print(NeuER);

}

void linie(float x2, float y2, float z2) {

  stroke(random(255), random(255), random(255));

  //if (mousePressed && (mouseButton == LEFT)) {
    //stroke(0, 0, 0, 0);
  //}

  line(0, 0, 0, x2, y2, z2);
  translate(x2, y2, z2);

}

void kreis (float x3, float y3, float z3 ) {
  fill(127, 4, 4);
   if (keyPressed && (key == 's')) {
    fill(255, 64,64);
  }
  //fill(random(100,150),random(200),random(40),200);
   if (keyPressed && (key == 'a')) {
     fill(238,238,0);
   }
   if (keyPressed && (key == 'd')) {
     fill(51,51,255);
   }
  noStroke();
  lights();
  translate(x3, y3, z3);
  sphere(2);
}



void rechts() {
  rotateZ(radians(25));
}

void links() {
  rotateZ(-(radians(25)));
}
void posMerk() {
  pushMatrix();
}

void posZrks() {
  popMatrix(); 
}

void vorKippen() {
  rotateX(radians(25));
}

void rueckKippen() {
  rotateX(-(radians(25)));
}

void vorRollen() {
  rotateY(radians(25));
}

void rueckRollen() {
  rotateY(-(radians(25)));
}

void PB() {
  rotate(PI*0.3);
}

void MB() {
  rotate(-(PI*(0.23)));
}

void draw() {
  sphereDetail(5);
  translate (640,980, 0);
  background(191,239,255,0); 
  if((mousePressed && (mouseButton == RIGHT))) {
    rotateX(radians(mouseX));
  }
  rotateY(radians(mouseX));
  float zoom = map(mouseY, 0, height, 0.1, 4);
  scale(zoom);
  linie(0, -20, 0);    //"Stamm" des Baum
  for (int i=0; i<NeuER.length(); i++) {
    if (NeuER.charAt(i)=='F') {
      linie(0, -8, 0);   //random(-100)    
     } else if (NeuER.charAt(i)=='[') {
      posMerk(); 
     } else if (NeuER.charAt(i)==']') { 
      posZrks(); 
     } else if (NeuER.charAt(i)=='-') {
      links();
     } else if (NeuER.charAt(i)=='+') {
      rechts(); 
     } else if (NeuER.charAt(i)=='&') {
      vorKippen();
     } else if (NeuER.charAt(i)=='^') {
      rueckKippen();
     } else if (NeuER.charAt(i)=='<') {
      vorRollen();
     } else if (NeuER.charAt(i)=='>') {
      rueckRollen();
     } else if (NeuER.charAt(i)=='L') {
      //scale(random(0.9,1.8));
      kreis(0, 0, 0);
     }
  }
}
Tagged:

Answers

  • `String Axiom="FL"; //"FX" String ER="F<F-[F^>>-F+F]+[>&+F-FL]"; //"FF-[F^-F+FL]+[>&+F-FL]" String ER2="F&F+<[F><[>+F]]+&[F<^&--FL]"; //"FF+[F&>+FL]+[F<^-FL]" String NeuER="";
    String Zwsp="";

    void setup() { size(1200, 1000, P3D); smooth(); strokeWeight(0.8); frameRate(300); for (int a=0; a<=5; a++) { if (a==0) { NeuER=NeuER+Axiom; } else { for (int b=0; b<NeuER.length(); b++) { if (NeuER.charAt(b)=='F') { Zwsp=Zwsp + ER; } else if (NeuER.charAt(b)=='X') { Zwsp=Zwsp + ER2; } else { Zwsp=Zwsp + NeuER.charAt(b); }
    } NeuER=Zwsp;
    Zwsp=""; } } print(NeuER);

    }

    void linie(float x2, float y2, float z2) {

    stroke(random(255), random(255), random(255));

    //if (mousePressed && (mouseButton == LEFT)) { //stroke(0, 0, 0, 0); //}

    line(0, 0, 0, x2, y2, z2); translate(x2, y2, z2);

    }

    void kreis (float x3, float y3, float z3 ) { fill(127, 4, 4); if (keyPressed && (key == 's')) { fill(255, 64,64); } //fill(random(100,150),random(200),random(40),200); if (keyPressed && (key == 'a')) { fill(238,238,0); } if (keyPressed && (key == 'd')) { fill(51,51,255); } noStroke(); lights(); translate(x3, y3, z3); sphere(2); }

    void rechts() { rotateZ(radians(25)); }

    void links() { rotateZ(-(radians(25))); } void posMerk() { pushMatrix(); }

    void posZrks() { popMatrix(); }

    void vorKippen() { rotateX(radians(25)); }

    void rueckKippen() { rotateX(-(radians(25))); }

    void vorRollen() { rotateY(radians(25)); }

    void rueckRollen() { rotateY(-(radians(25))); }

    void PB() { rotate(PI*0.3); }

    void MB() { rotate(-(PI*(0.23))); }

    void draw() { sphereDetail(5); translate (640,980, 0); background(191,239,255,0); if((mousePressed && (mouseButton == RIGHT))) { rotateX(radians(mouseX)); } rotateY(radians(mouseX)); float zoom = map(mouseY, 0, height, 0.1, 4); scale(zoom); linie(0, -20, 0); //"Stamm" des Baum for (int i=0; i<NeuER.length(); i++) { if (NeuER.charAt(i)=='F') { linie(0, -8, 0); //random(-100)
    } else if (NeuER.charAt(i)=='[') { posMerk(); } else if (NeuER.charAt(i)==']') { posZrks(); } else if (NeuER.charAt(i)=='-') { links(); } else if (NeuER.charAt(i)=='+') { rechts(); } else if (NeuER.charAt(i)=='&') { vorKippen(); } else if (NeuER.charAt(i)=='^') { rueckKippen(); } else if (NeuER.charAt(i)=='<') { vorRollen(); } else if (NeuER.charAt(i)=='>') { rueckRollen(); } else if (NeuER.charAt(i)=='L') { //scale(random(0.9,1.8)); kreis(0, 0, 0); } } }

  • you can just store all line colors since the tree is static

  • String Axiom="FL"; //"FX" 
    String ER="F<F-[F^>>-F+F]+[>&+F-FL]"; //"FF-[F^-F+FL]+[>&+F-FL]" 
    String ER2="F&F+<[F><[>+F]]+&[F<^&--FL]"; //"FF+[F&>+FL]+[F<^-FL]" 
    String NeuER="";
    String Zwsp="";
    
    // plan fix colors 
    int maxC = 19999; 
    color[] colorList = new color[ maxC]; 
    int counting; // for colorList  
    
    void setup() { 
      size(1200, 1000, OPENGL); 
    
      smooth(); 
      strokeWeight(0.8); 
      frameRate(70);
    
      for (int i = 0; i <  maxC; i++) {
        colorList[i] = color(random(255), random(255), random(255));
      }
    
      for (int a=0; a<=5; a++) { 
        if (a==0) { 
          NeuER=NeuER+Axiom;
        } else { 
          for (int b=0; b<NeuER.length (); b++) { 
            if (NeuER.charAt(b)=='F') { 
              Zwsp=Zwsp + ER;
            } else if (NeuER.charAt(b)=='X') { 
              Zwsp=Zwsp + ER2;
            } else { 
              Zwsp=Zwsp + NeuER.charAt(b);
            }
          } 
          NeuER=Zwsp;
          Zwsp="";
        }
      } 
      print(NeuER);
    }
    
    void linie(float x2, float y2, float z2) {
    
    
      stroke(colorList[counting]);
    
      //  stroke(random(255), random(255), random(255));
      //  stroke(222); 
      //if (mousePressed && (mouseButton == LEFT)) { //stroke(0, 0, 0, 0); //}
    
      line(0, 0, 0, x2, y2, z2); 
      translate(x2, y2, z2);
    
      counting++;
    }
    
    void kreis (float x3, float y3, float z3 ) { 
      fill(127, 4, 4); 
      if (keyPressed && (key == 's')) { 
        fill(255, 64, 64);
      } 
      //fill(random(100,150),random(200),random(40),200); 
      if (keyPressed && (key == 'a')) { 
        fill(238, 238, 0);
      } 
      if (keyPressed && (key == 'd')) { 
        fill(51, 51, 255);
      } 
      noStroke(); 
      lights(); 
      translate(x3, y3, z3); 
      sphere(2);
    }
    
    void rechts() { 
      rotateZ(radians(25));
    }
    
    void links() { 
      rotateZ(-(radians(25)));
    } 
    void posMerk() { 
      pushMatrix();
    }
    
    void posZrks() { 
      popMatrix();
    }
    
    void vorKippen() { 
      rotateX(radians(25));
    }
    
    void rueckKippen() { 
      rotateX(-(radians(25)));
    }
    
    void vorRollen() { 
      rotateY(radians(25));
    }
    
    void rueckRollen() { 
      rotateY(-(radians(25)));
    }
    
    void PB() { 
      rotate(PI*0.3);
    }
    
    void MB() { 
      rotate(-(PI*(0.23)));
    }
    
    void draw() { 
      counting=0;
      sphereDetail(5); 
      translate (640, 980, 0);
      // noLoop(); 
      background(191, 239, 255, 0); 
      if ((mousePressed && (mouseButton == RIGHT))) { 
        rotateX(radians(mouseX));
      } 
      rotateY(radians(mouseX)); 
      float zoom = map(mouseY, 0, height, 0.1, 4); 
      scale(zoom); 
      linie(0, -20, 0); 
      //"Stamm" des Baum 
      for (int i=0; i<NeuER.length (); i++) { 
        if (NeuER.charAt(i)=='F') { 
          linie(0, -8, 0); //random(-100)
        } else if (NeuER.charAt(i)=='[') { 
          posMerk();
        } else if (NeuER.charAt(i)==']') { 
          posZrks();
        } else if (NeuER.charAt(i)=='-') { 
          links();
        } else if (NeuER.charAt(i)=='+') { 
          rechts();
        } else if (NeuER.charAt(i)=='&') { 
          vorKippen();
        } else if (NeuER.charAt(i)=='^') { 
          rueckKippen();
        } else if (NeuER.charAt(i)=='<') { 
          vorRollen();
        } else if (NeuER.charAt(i)=='>') { 
          rueckRollen();
        } else if (NeuER.charAt(i)=='L') { 
          //scale(random(0.9,1.8));
          kreis(0, 0, 0);
        }
      }
    }
    
  • you could also make a class myLine with color and coords and an ArrayList of that

    first hammer all lines into it, probably with spheres and that in setup

    in draw just display the ArrayList

  • you're choosing a random colour for every line with every new frame.

    line 36 is to blame but i wouldn't know how to fix it

    (maybe, have an arrayList of random colours, one for each line. call stroke before every line using the colour for that line. probably better to define a shape using beginShape / endShape in the first frame and draw that shape for ever subsequent frame)

  • maybe not:

    "Transformations such as translate(), rotate(), and scale() do not work within beginShape()."

  • in theory this version starts faster and is then slower

    but uses an array just the right size now

    String Axiom="FL"; //"FX" 
    String ER="F<F-[F^>>-F+F]+[>&+F-FL]"; //"FF-[F^-F+FL]+[>&+F-FL]" 
    String ER2="F&F+<[F><[>+F]]+&[F<^&--FL]"; //"FF+[F&>+FL]+[F<^-FL]" 
    String NeuER="";
    String Zwsp="";
    
    // plan fix colors  
    int maxC; 
    color[] colorList; 
    int counting; // for the colorList  
    
    void setup() { 
      size(1200, 1000, OPENGL); 
    
      smooth(); 
      strokeWeight(0.8); 
      frameRate(70);
    
      //for (int i = 0; i <  maxC; i++) {
      //   colorList[i] = color(random(255), random(255), random(255));
      //}
    
      for (int a=0; a<=5; a++) { 
        if (a==0) { 
          NeuER=NeuER+Axiom;
        } else { 
          for (int b=0; b<NeuER.length (); b++) { 
            if (NeuER.charAt(b)=='F') { 
              Zwsp=Zwsp + ER;
            } else if (NeuER.charAt(b)=='X') { 
              Zwsp=Zwsp + ER2;
            } else { 
              Zwsp=Zwsp + NeuER.charAt(b);
            }
          } 
          NeuER=Zwsp;
          Zwsp="";
        }
      } 
    
      println(NeuER);
      //  println("length = "+NeuER.length());
      maxC = countingInAString (NeuER, "F") + 1; 
      colorList = new color[ maxC];
      println("end of setup");
    }
    
    int countingInAString ( String longString, String search) {
    
      // small function 
      // how many F are in NeuER ? 
    
      println(longString.contains(search));
      String test1 = longString.replaceAll ( search, "");
      println(test1);
    
      println (longString.length()-test1.length());
    
      return longString.length()-test1.length();
    }
    
    void linie(float x2, float y2, float z2) {
    
      // if not intialised 
      if (brightness(colorList[counting]) <= 0 ) {
        // assign a value
        colorList[counting] = color(random(255), random(255), random(255));
      }
    
      stroke(colorList[counting]);
    
      //  stroke(random(255), random(255), random(255));
      //  stroke(222); 
      //if (mousePressed && (mouseButton == LEFT)) { //stroke(0, 0, 0, 0); //}
    
    
      line(0, 0, 0, x2, y2, z2); 
      translate(x2, y2, z2); 
    
      counting++;
    }
    
    void kreis (float x3, float y3, float z3 ) { 
      fill(127, 4, 4); 
      if (keyPressed && (key == 's')) { 
        fill(255, 64, 64);
      } 
      //fill(random(100,150),random(200),random(40),200); 
      if (keyPressed && (key == 'a')) { 
        fill(238, 238, 0);
      } 
      if (keyPressed && (key == 'd')) { 
        fill(51, 51, 255);
      } 
      noStroke(); 
      lights(); 
      translate(x3, y3, z3); 
      sphere(2);
    }
    
    void rechts() { 
      rotateZ(radians(25));
    }
    
    void links() { 
      rotateZ(-(radians(25)));
    } 
    void posMerk() { 
      pushMatrix();
    }
    
    void posZrks() { 
      popMatrix();
    }
    
    void vorKippen() { 
      rotateX(radians(25));
    }
    
    void rueckKippen() { 
      rotateX(-(radians(25)));
    }
    
    void vorRollen() { 
      rotateY(radians(25));
    }
    
    void rueckRollen() { 
      rotateY(-(radians(25)));
    }
    
    void PB() { 
      rotate(PI*0.3);
    }
    
    void MB() { 
      rotate(-(PI*(0.23)));
    }
    
    void draw() { 
      counting=0;
      sphereDetail(5); 
      translate (640, 980, 0);
      // noLoop(); 
      background(191, 239, 255, 0); 
      if ((mousePressed && (mouseButton == RIGHT))) { 
        rotateX(radians(mouseX));
      } 
      rotateY(radians(mouseX)); 
      float zoom = map(mouseY, 0, height, 0.1, 4); 
      scale(zoom); 
      linie(0, -20, 0); 
      //"Stamm" des Baum 
      for (int i=0; i<NeuER.length (); i++) { 
        if (NeuER.charAt(i)=='F') { 
          linie(0, -8, 0); //random(-100)
        } else if (NeuER.charAt(i)=='[') { 
          posMerk();
        } else if (NeuER.charAt(i)==']') { 
          posZrks();
        } else if (NeuER.charAt(i)=='-') { 
          links();
        } else if (NeuER.charAt(i)=='+') { 
          rechts();
        } else if (NeuER.charAt(i)=='&') { 
          vorKippen();
        } else if (NeuER.charAt(i)=='^') { 
          rueckKippen();
        } else if (NeuER.charAt(i)=='<') { 
          vorRollen();
        } else if (NeuER.charAt(i)=='>') { 
          rueckRollen();
        } else if (NeuER.charAt(i)=='L') { 
          //scale(random(0.9,1.8));
          kreis(0, 0, 0);
        }
      }
      //  println ("max counting " + counting);
    }
    
  • I was reminded recently that noise returns the same 'random' number when fed the same input and thought that might be useful for almost exactly this situation... You could use array index as input, but would of course have to convert the returned value to a color somehow.

    Probably setting colors up in advance and storing them is more sensible...

Sign In or Register to comment.