xxxxxxxxxx
let x,y;
let px, py;
let step = 1;
let stepSize = 20;
let numSteps = 1;
let state = 0;
let turnCounter = 0;
function isPrime(value) {
if (value == 1) return false;
for (let i = 2; i <= sqrt(value); i++) {
if (value % i == 0) {
return false;
}
}
return true;
}
function setup() {
createCanvas(windowWidth, windowHeight);
x = width / 2;
y = height / 2;
px = x;
py = y;
background(0);
}
function draw() {
text(step, x, y);
fill(50);
stroke(50);
rectMode(CENTER);
square(x, y, stepSize);
fill(255,255,0);
stroke(255,255,0);
strokeWeight(1);
line(x, y, px, py);
px = x;
py = y;
fill(255,255,0);
stroke(255,255,0);
if (isPrime(step)) {
circle(x, y, stepSize * 0.75);
fill(255,0,0);
stroke(255,0,0);
textSize(stepSize/2);
textAlign(CENTER,CENTER);
text(step, x, y);
}
//SWITCH
switch(state){case 0:x+=stepSize;break;case 1:y-=stepSize;break;case 2:x-=stepSize;break;case 3:y+=stepSize}
if (step % numSteps == 0) {
state = (state + 1) % 4;
turnCounter++;
if (turnCounter % 2 == 0) {
numSteps++;
}
if (turnCounter == 70) {
noLoop();
}
}
step++;
}