Grid-o-spirals

Simple spirals

Log in to post a comment.

// LL 2021

Canvas.setpenopacity(1);

const turtle = new Turtle();

const grid = 9;              // min=1, max=20, step=1
const outer = 151;           // min=10, max=1000, step=1
const inner = 20;            // min=0, max=1000, step=1
const startAngle = 1.4;      // min=0, max=6.2831853072, step=0.001
const angleIncCoarse = 0.37; // min=0.0, max=6.2831853072, step=0.001
const angleIncFine = -0.03;  // min=-0.05, max=0.05, step=0.00001
const angleIncInc  = 0.0704; // min=0.0, max=0.1, step=0.0001
const scale = 219;           // min=1, max=1000, step=1

const canvas_size = 100;

const count = outer - inner;


// The walk function will be called until it returns false.
function walk(i)
{
    if (i >= grid * grid * count) return false;
    
    const index = Math.floor(i / count);

    const grid_x = index % grid;
    const grid_y = Math.floor(index / grid);
    
    const center_x = -canvas_size + canvas_size/grid + canvas_size*2/grid * grid_x;
    const center_y = -canvas_size + canvas_size/grid + canvas_size*2/grid * grid_y;

    if ((i%count) == 0)
    {
        turtle.penup();

        if (true) // draw a border around each cell
        {
            const size = canvas_size / grid * 0.95;
            turtle.goto( center_x - size, center_y - size );
            turtle.pendown();
            turtle.goto( center_x + size, center_y - size );
            turtle.goto( center_x + size, center_y + size );
            turtle.goto( center_x - size, center_y + size );
            turtle.goto( center_x - size, center_y - size );
            turtle.penup();
        }
    }

    const angleIncCoarse2 = angleIncCoarse + index * angleIncInc;

    const i2 = i % count;
    const radius = (i2+inner) * (i2+inner) / scale / grid;
    const angle = startAngle + i2 * (angleIncCoarse2 + angleIncFine);
    turtle.goto( center_x + Math.cos(angle) * radius, center_y + Math.sin(angle) * radius)

    turtle.pendown();
    
    //sleep(1);

    return true;
}

////////////////////////
// Utils
////////////////////////

function sleep(milliseconds) {
  const date = Date.now();
  let currentDate = null;
  do {
    currentDate = Date.now();
  } while (currentDate - date < milliseconds);
}