Snowflake 002

Draws a single Snowflake with random twig lengths

Log in to post a comment.

// Forked from "Snowflake 001 " by rupertxrussell
// https://turtletoy.net/turtle/45cd645993

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(1);

// Global code will be evaluated once.
const arms = 10; //min=4 max=10 step=1
const armLength = 55; //min=10 max=80 step=1
const numTwigs = 8; //min=3 max=8 step=1
const maxTwigLength = 14; //min=0 max=25 step=1
const minTwigLength = 0; //min=0 max=25 step=1
var twigLength;
const turtle = new Turtle();

let twig = [];

// calculate a random twig length for each twig

turtle.penup();
turtle.goto(-50,-50);
turtle.pendown();

for (let nTwig = 0; nTwig <= numTwigs; nTwig ++){
    twig[nTwig] = getRndInteger(minTwigLength, maxTwigLength);
}


for (let i = 0; i < arms ; i++) {
    turtle.penup();
    turtle.goto(-0,-0);
    turtle.pendown();
    turtle.right(360/arms);
   
   
    for(let n = 0; n < numTwigs; n ++){
        turtle.forward(armLength / numTwigs);
        turtle.right(360/arms);
        
        turtle.forward(twig[n]);
        turtle.back(twig[n]);
        turtle.left(360/arms * 2);
        
        turtle.forward(twig[n]);
        turtle.back(twig[n]);
        turtle.right(360/arms);
        
    }
   turtle.forward(twig[0]);

}

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) + min;
}