Snowflakes

Makes a grid of slowflakes, with a few options that should be clear. Other option is to set the grid to be regular - do this by removing randomSize from turtle.forward in the main loop.

Log in to post a comment.

Canvas.setpenopacity(1);

const turtle = new Turtle();
turtle.penup();
// put the grid centre in the bottom left (screen is 200x200)
const centre_x = -80;
const centre_y = 80;
turtle.goto(centre_x, centre_y);
turtle.pendown();

// change number of snowflake arms
var num_sides = 5;
var snow_angle = 360 / num_sides;
const random_sides = true;

// rotate randomly?
const random_angle = true;

// size

// grid set up
const gap = 24;
const rows = 5;
const cols = 5;

// Draw a tessellation of snowflakes
for (let i = 0; i < rows; i++) {
    for (let j = 0; j < cols; j++) {
        const randomSize = Math.floor(Math.random() * 10) + 12; // Random size (edit range)
        drawSnowflake(turtle, randomSize, random_angle, random_sides);
        turtle.penup();
        turtle.forward(randomSize + gap);
        turtle.pendown();
    }
    turtle.penup();
    // 
    turtle.goto(centre_x, centre_y - (i + 1) * (15 + gap)); // Adjusted for a typical size of 15
    turtle.pendown();
}

function drawSnowflake(turtle, size, random_angle, random_sides) {
    // randomly rotate
    if (random_angle){
        var angle = Math.floor(Math.random() * 90);
        turtle.right(angle)
    } else {
    var angle = 0;
    }
    
    if (random_sides) {
        var num_sides = Math.floor(Math.random() * 5) + 5;
        var snow_angle = 360 / num_sides;
    }
    
    // random number of outer flakes
    var outer_sides = Math.floor(Math.random()*3) + 3;
    for (let i = 0; i < num_sides; i++) {
        drawSnowflakeArm(turtle, size, outer_sides);
        turtle.right(snow_angle);
    }
    // return back to normal direction
    turtle.left(angle);
    
}

function drawSnowflakeArm(turtle, size, outer_sides) {
    const armLength = size / 2;
    const subArmLength = size / 6;
    turtle.forward(armLength);
    
    for (let j = 0; j < outer_sides; j++) {
        turtle.forward(subArmLength);
        turtle.backward(subArmLength);
        turtle.right(360/outer_sides);
    }

    turtle.backward(armLength);
}