Scribble Knoted String

clumps of randomness

Log in to post a comment.

Canvas.setpenopacity(1);

const turtle = new Turtle();

const MARGIN = 15; // min=0, max=50, step=1
const COUNT_PER_ROW=5; // min=1, max=25, step=1
const MIN_POINTS = 15; // min=4, max=100, step=1
const MAX_POINTS = 80; // min=4, max=100, step=1
const SIZE=200, BOX_SIZE = (SIZE - (MARGIN * (COUNT_PER_ROW + 1))) / COUNT_PER_ROW, MBOX_SIZE = BOX_SIZE + MARGIN, CENTER=SIZE / 2;

turtle.jump(MARGIN - CENTER, MARGIN - CENTER);
turtle.pendown();

const goto = (x, y) => turtle.goto(x - CENTER, y - CENTER);

const rand = (min, max) => Math.random() * (max - min) + min;

let minX=MARGIN, minY=MARGIN;
function walk(k) {
  for(let i=rand(MIN_POINTS, MAX_POINTS) ; i >= 0 ; i--) {
    const x = rand(minX, minX + BOX_SIZE), y = rand(minY, minY + BOX_SIZE);
    goto(x, y);
  }

  goto(minX + BOX_SIZE / 2, minY + BOX_SIZE / 2);

  if((k + 1) % COUNT_PER_ROW === 0) {
    minX += MBOX_SIZE;
  } else {
    const isOddRow = ! ((k + 1) / COUNT_PER_ROW & 1);
    minY = minY + ((isOddRow ? 1 : -1) * MBOX_SIZE);
  }

  const notLast = (k + 1) < (COUNT_PER_ROW * COUNT_PER_ROW)
  notLast && goto(minX + BOX_SIZE / 2, minY + BOX_SIZE / 2);

  return notLast;
}