I managed to get Chat GPT to fix the code after I showed it the output from Recamán's sequence by @davidhg
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.
const turtle = new Turtle();
let N = 64; //min=1 max=900 step=1
let scale = 2.7; //min=0.1 max=5 step=0.1
let x = -90; //min=-200 max=0 step=1
let sides = 2; //min=2 max=30 step=1
let seen = new Set();
let a = 0;
let y = 0;
seen.add(a);
// Improved Recamán's Sequence
turtle.pendown();
// Move to start
turtle.penup();
turtle.goto(x, y);
turtle.pendown();
for (let n = 1; n <= N; n++) {
let candidate = a - n;
let b = (candidate > 0 && !seen.has(candidate))
? candidate
: a + n;
let x2 = x + (b - a) * scale;
let r = Math.abs(x2 - x) / 2;
let cx = (x + x2) / 2;
drawSemicircle(cx, y, r, n % 2 === 0);
x = x2;
a = b;
seen.add(a);
}
// ---- helpers ----
function drawSemicircle(cx, cy, r, up) {
let start = up ? 0 : 180;
let dir = up ? 1 : -1;
turtle.penup();
turtle.goto(cx - r, cy);
turtle.setheading(0);
for (let i = 0; i <= sides; i++) {
let t = Math.PI * i / sides;
let px = cx + r * Math.cos(t);
let py = cy + dir * r * Math.sin(t);
turtle.goto(px, py);
turtle.pendown();
}
}