Moving Snowflake

Hi,

I have created Snowflake animation.

Tagged:

Answers

  • Make a snowflake class and create an array of snowflake objects. Why not?

  • You might be interested in some of the reference Example sketches online: things like clicking to create multiple falling objects, particle systems, etc.

  • Currently in my setup(), I have set the size, background, stroke and a for loop to get the final state of the object after substituting the rule into the turtle.

    for(int k = 0; k < L; k++){ state = substitute(state, ruleF); }

    How can I make it into an array?

  • the final state of the object after substituting the rule into the turtle.

    It is not clear what you are asking. Please remember we don't know anything about your code unless you explicitly show it to us so to be able to understand what you are trying to do. This is a good opportunity to check what a MCVE is: https://stackoverflow.com/help/mcve

    Kf

  • @JamesKoo -- yes, this is unclear. What is state? What is substitute()? What is ruleF? What is "the turtle"?

    Share your code here -- you can format it for a forum post following these instructions: https://forum.processing.org/two/discussion/15473/how-to-format-code-and-text

  • here is a example to place two snowflakes

    LSystem[] ps=new LSystem[2];
    
    void setup() {
      size(640, 360);
      ps[0] = new LSystem();
      ps[0].simulate(3);
      ps[1] = new LSystem();
      ps[1].simulate(3);
    }
    
    void draw() {
      // background(0);
      ps[0].render();
      ps[1].render();
    }
    
    // ==================================================
    
    class LSystem {
    
      int steps = 12;
    
      String axiom;
      String rule;
      String production;
    
      float startLength=100;
      float drawLength;
      float theta;
    
      int generations;
    
      float x=random(width-44), y=random(height-44);
    
      LSystem() {
    
        axiom = "F";
        rule = "F+F";
        startLength = 90.0;
        theta = radians(120.0);
        reset();
      }
    
      void reset() {
        production = axiom;
        drawLength = startLength;
        generations = 0;
      }
    
      int getAge() {
        return generations;
      }
    
      void render() {
        translate(x, y);
        steps += 5;          
        if (steps > production.length()) {
          steps = production.length();
        }
        for (int i = 0; i < steps; i++) {
          char step = production.charAt(i);
          if (step == 'F') {
            fill(255);
            rect(0, 0, -drawLength, -drawLength);
            noFill();
            translate(0, -drawLength);
          } else if (step == '+') {
            rotate(theta);
          } else if (step == '-') {
            rotate(-theta);
          } else if (step == '[') {
            pushMatrix();
          } else if (step == ']') {
            popMatrix();
          }
        }
      }
    
      void simulate(int gen) {
        while (getAge() < gen) {
          production = iterate(production, rule);
        }
      }
    
      String iterate(String prod_, String rule_) {
        drawLength = drawLength * 0.6;
        generations++;
        String newProduction = prod_;          
        newProduction = newProduction.replaceAll("F", rule_);
        return newProduction;
      }
      //
    }
    //
    
  • edited March 2018

    Currently I have create a snowflakes class file, but unable to make it into an array to redraw it randomly.

    tes[] flakes = new tes[100];
    
    void setup() {
      size(600, 400);
      background(255);
      stroke(0);
      flakes[i] = new tes();
    }
    
    void draw() {
      background(255);
      //tes flakes = new tes();
      y = y + speed;
      if (y>height+100) {
        y = -10;
        L = 3; //Set value of L back to 3 when it redraw.
      }
      //flakes.animate();
      //flakes.display();
      for (int i =0; i<100; i++) {
        flakes[i].draw();
        flakes[i].mouseClicked();
      }
    }
    
    
    ----------------------------------------------------
      tes class file
    
    
    int SD = 5, sd = SD/2;
    float d = sd;
    float angle = PI/3;
    String state = "[F+F+F+F+F]";
    String ruleF = "F-F++F-F";
    float L = 3;
    float fade = 1;
    float speed = random(1, 5);
    float x = random(0, 600);
    float y = 100;
    
    public class tes {
    
      void draw() {
        pushMatrix();
        fade++;
        stroke(fade, 255);
          for (int k = 0; k < L; k++) {
            state = substitute(state, ruleF);
          }
        }
        single();
        popMatrix();
      }
    
      void mouseClicked() {
        L=L-1;
      }
    
      void single() {
        translate(x, y);
        for (int i =0; i < state.length(); i++)
          turtle(state.charAt(i));
      }
    
    }
    

    How can i change it to an array of snowflakes?

  • edited March 2018

    I demonstrated that above with LSystem class

    Move this line

      tes flakes = new tes();
    

    before setup

    And change it to

     tes[] flakes=new tes[100];
    

    Now in setup() use a for loop similar to my setup

    Inside the for loop

    flakes[i] = new tes(......

    in draw also use a for loop and then similar to my draw () above

    flakes[i].render();

  • I have just did some adjustment on my codes on top, is that what you mean?

  • Does it work?

    Does it do what you want?

  • edited March 2018

    You are almost there

    You need this in setup() too :

    for(int i =0; i<100;i++)

    Before flakes[....

    Do you know why?

    ;-)

  • I edited the codes on my phone, have yet to try it out yet. I don't have my computer with my currently.

    If not, it will generate only one flakes right?

  • It will crash

  • edited March 2018

    You need to make additional changes too:

    • Since all the snowflakes should have different positions you need to move x,y inside the class

    • So each snowflake is individual

    • In fact move all or as much as you can of the global variables inside the class

    Remark

    It would be cool to have different types of flakes, maybe you can vary the rule for each but that’s for later

  • edited March 2018

    Currently my number of iteration is set to 3, also know as the value "L". I tried to add on a mouseClicked() function to decrease the number of iteration. However when I clicked on my mouse, the value of L decrease to 2 but it doesn't update the value of L in the display() of the class file. May i know where have i gone wrong?

  • post your entire code to check

  • Is L a variable inside the class or a global one?

  • Currently it is global. I update it on 9th post on top.

  • Answer ✓

    in setup() use a for loop similar to my setup

    Inside the for loop

    flakes[i] = new tes(......

    in draw also use a for loop and then similar to my draw () above

    flakes[i].render();

  • you haven't moved the variables inside the class as I told you

    also better post the entire code a the bottom of the discussion as new post and not edit old posts, because then my remarks about the code don't make sense anymore...

    and the whole discussion is just confusing

  • I got a running version with 100 flakes.

    Do you have that too?

    (no mouseClicked() function or L change)

  • I have shift it to a new discussion to avoid further confusion. Sorry about that.

  • edited March 2018

    Could you pm me on that? I would like to see the different.

  • Why did you start a new discussion? Pointless. Even more so since in the new discussion your code is not runnable. Bad.

    Question:

    I got a running version with 100 flakes.

    Do you have that too?

  • So what do i do currently? Update this post?

  • 100 snowflakes running with the lsystem?

  • start by answer my question

  • edited March 2018

    post your entire sketch here.

    a runnable version

    when you want to change L what do you want to achieve in the graphic?

Sign In or Register to comment.