THING

From "Turtle Geometry" by Harold Abelson, 1980

Variations from the book:
"THING1": THING (variation)
"THING2": THING (variation)
"THING3": THING (variation)

Log in to post a comment.

const right = -54; // min=-60, max=60, step=1
const forward = 40; // min=0, max=100, step=5
const repeat = 15; // min=1, max=64, step=1
const scale = 0.6; // min=0.1, max=1.0, step=0.05
const center = 1; // min=0, max=1, step=1 (No, Yes)

const t = new Turtle();

if (center) {
    // Compensate for offset by calculating center of regular polygon.

    // At full scale, each step travels (125, 25).
    // Add forward to x.
    // Interior angle is 90 + right.
    t.penup();
    t.radians();
    
    const d2r = deg => deg / 180 * Math.PI;
    const [x, y] = [(125+forward)*scale, 25*scale];
    const sidelength = Math.sqrt(x**2 + y**2);
    const interior_angle = d2r(90 + right);
    const angle_offset = Math.atan2(y, x);
    const circumradius = sidelength / 2 / Math.cos(interior_angle/2);

    console.debug({x, y, sidelength, interior_angle, angle_offset, circumradius});

    t.right(angle_offset);
    t.left(interior_angle/2);
    t.backward(circumradius);
    
    console.debug("Offset by", t.pos());
    
    t.setheading(0);
    t.degrees();
    t.pendown();
}

function thing() {
    t.forward(scale*100);
    t.right(90);
    t.forward(scale*100);
    t.right(90);
    t.forward(scale*50);
    t.right(90);
    t.forward(scale*50);
    t.right(90);
    t.forward(scale*100);
    t.right(90);
    t.forward(scale*25);
    t.right(90);
    t.forward(scale*25);
    t.right(90);
    t.forward(scale*50);
}

function walk(i) {
    t.forward(scale*forward); // move forward first to make center calculations simpler
    thing();
    t.right(right);
    return i+1 < repeat;
}