Space filling curves

Randomized strings of arcs that fill up an invisible grid. If a square is occupied by an arc, then another arc will not be drawn there so they fill the space without over cluttering.
Reminds me of the texture of a leaf.

Log in to post a comment.

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

// Global code will be evaluated once.
console.clear();
const turtle = new Turtle();
let seed = 1000; // min=1 max=1000000 step=1
const bits = 20; 
const samples = 150000; 
const mod = 1<<bits;
let done = false;
let num_lines = 600; // min = 10 max = 600 step = 1
let arcs_per_line = 5; // min = 1 max = 10 step = 1
let curviness = 2; // min = 0 max = 30 step = 1
let steps_per_arc = 40; // min = 1 max = 100 step = 1
let draw_squares_filled = 0; //min = 0 max = 1 step = 1 (No, Yes)

already_visited = [];
for(let i = -100; i < 100; i++) {
    already_visted_row = []
    for(let j = -100; j < 100; j++) {
        already_visted_row.push(false);
    }
    already_visited.push(already_visted_row);
}

function arc(steps, angle) {
    last_x = 110;
    last_y = 110;
    for(let i = 0; i < steps; i++) {
        turtle.forward(1);
        turtle.right(angle);
        // console.log("x: " + Math.round(turtle.x()) + ", y: " + + Math.round(turtle.y()))
        if(Math.abs(Math.round(turtle.x())) >= 100 || Math.abs(Math.round(turtle.y())) >= 100) {
            done = true;
            break;
        }
        else if(last_x == Math.round(turtle.x())+100 && last_y == Math.round(turtle.y())+100 || !already_visited[Math.round(turtle.x())+100][Math.round(turtle.y())+100]) {
            last_x = Math.round(turtle.x())+100;
            last_y = Math.round(turtle.y())+100;
            already_visited[Math.round(turtle.x())+100][Math.round(turtle.y())+100] = true;
        }
        else {
            done = true;
            break;
        }
    }
}

function circle(radius, steps, x_center, y_center) {
    theta = Math.PI * 2 / steps;
    turtle.penup();
    turtle.goto(x_center + radius * Math.cos(theta * 0), y_center + radius * Math.sin(theta * 0))
    turtle.pendown();
    for(i = 0; i <= steps; i++) {
        x = x_center + radius * Math.cos(theta * i);
        y = y_center + radius * Math.sin(theta * i);
        turtle.goto(x,y);
    }
}

function square(side_length, x_center, y_center) {
    turtle.penup();
    turtle.goto(x_center + side_length/2, y_center + side_length/2);
    turtle.pendown();
    turtle.goto(x_center - side_length/2, y_center + side_length/2);
    turtle.goto(x_center - side_length/2, y_center - side_length/2);
    turtle.goto(x_center + side_length/2, y_center - side_length/2);
    turtle.goto(x_center + side_length/2, y_center + side_length/2);
}

for(let i = 0; i < num_lines; i++) {
    done = false;
    turtle.jump(random() * 200 - 100, random() * 200 - 100)
    for(let j = 0; j < arcs_per_line; j++) {
        if(!done) {
            arc(Math.round(random() * steps_per_arc), Math.round(random() * curviness * 2 - curviness));
        }
    }
}

if(draw_squares_filled) {
    for(let i = -100; i < 100; i++) {
        for(let j = -100; j < 100; j++) {
            if(already_visited[i+100][j+100]) {
                square(1, i, j);
            }
        }
    }
}

////////////////////////////////////////////////////////////////
// Pseudorandom number generator. Created by Reinder Nijhoff 2024
// https://turtletoy.net/turtle/a2274fd1fe
////////////////////////////////////////////////////////////////
function random() { // returns a number [0, 1[
    let r = 1103515245 * (((seed+=12345) >> 1) ^ seed);
    r = 1103515245 * (r ^ (r >> 3));
    r = r ^ (r >> 16);
    return (r % mod) / mod;
}