> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.ZpJqD1U4la1/rev.851
 * 
 * authors: 
 *   
 *   Chris Wren

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



Turtle t;
color bgColor = #000000;
int level = 0;
float side0 = 650;
void setup() {
    size(800, 800);
    background(bgColor);
    t = new Turtle();
    frameRate(1);
}
void draw() {
    background(bgColor);
    t.penup();
    t.home();
    t.setxy(-side0 / 2,  2 * sin (PI / 3) * side0 / 6 );
    t.right(30);
    t.pendown();
    snowflake(level, side0 );
    t.right(120);
    snowflake(level, side0 );
    t.right(120);
    snowflake(level, side0 );
    t.right(120);
    level = level + 1;
    if (pow(2, level + 1) > side0) {
        noLoop();
    }
}
 
void snowflake(int level, float side) {
    if (level == 0) {
        t.forward(side);
    } else {
        float side1 = side / 3;
        snowflake(level - 1, side1);
        t.left(60);
        snowflake(level - 1, side1);
        t.right(120);
        snowflake(level - 1, side1);
        t.left(60);
        snowflake(level - 1, side1);
    }
}
// stolen form http://www.openprocessing.org/sketch/26896
// and modified to support floating point positions.
class Turtle {
    float x,y;
    float oldx,oldy;
    float angle;
    color tcolor;
     
    //Constructor
    Turtle() {
        oldx = int(width/2);
        oldy = int(height/2);
        x = oldx;
        y = oldy;
        tcolor = #FFFFFF;
        angle = 0;
        stroke (tcolor);
    }
     
    void forward (int step) {
        x = oldx - (step * cos(radians(angle+90)));
        y = oldy - (step * sin(radians(angle+90)));
        line(oldx,oldy,x,y);
        oldx = x;
        oldy = y;
    }
 
    void back (int step) {
        x = oldx + (step * cos(radians(angle+90)));
        y = oldy + (step * sin(radians(angle+90)));
        line(oldx,oldy,x,y);
        oldx = x;
        oldy = y;
    }
 
    void home () {
        x = int(width/2);
        y = int(height/2);
        line(oldx,oldy,x,y);
        oldx = x;
        oldy = y;
        angle = 0.0;
    }
 
    void setx(int step) {
        x = oldx + step;
        oldx = x;
    }
 
    void sety(int step) {
        y = oldy + step;
        oldy = y;
    }
 
    void setxy(int stepx, int stepy) {
        x = oldx + stepx;
        y = oldy + stepy;
        oldx = x;
        oldy = y;
    }
     
    void left (int dangle) {
        angle -= dangle;
    }
 
    void right (int dangle) {
        angle += dangle;
    }
 
    void setheading (int nangle) {
        angle = nangle;
    }
     
    void pencolor (color ncolor) {
        tcolor = ncolor;
        stroke (tcolor);
    }
     
    void penup() {
        noStroke();
    }
 
    void pendown() {
        stroke (tcolor);
    }
 
    void penerase() {
        stroke (bgColor);
    }
}