Circles cricles

Circles

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(0.5);
const translateX = -0.0;  // min=-5 max=5 step=0.01 (horizontal translation step)
const translateY = -0.0; // min=-5 max=5 step=0.01 (vertical translation step)
const translateOriginX = 0.5;  // min=-0.5 max=0.5 step=0.01 (horizontal translation step)
const translateOriginY = -0.3; // min=-0.5 max=0.5 step=0.01 (vertical translation step)
const scaleDecay = 0.23; // min=0 max=1 step=0.01 (shrinking factor for scaling)
const scale = 0.01; // min=0.001 max=0.020 step=0.001 (shrinking factor for scaling)
const rotationStep = -1.8; // min=-2 max=2 step=0.01 (rotation per iteration in radians)
const steps = 705; // min=1 max=10000 step=1 (number of iterations)

// Global code will be evaluated once.
const turtle = new Turtle();
turtle.penup();
turtle.goto(-50,-20);
turtle.pendown();

function Translate(x,y) { return p => [p[0]+x, p[1]+y]; }
function Rotate(a) { return p => [p[0]*Math.cos(a)+p[1]*Math.sin(a), p[1]*Math.cos(a)-p[0]*Math.sin(a)]; }
function Scale(s) { return p => [p[0]*s, p[1]*s]; }

// The walk function will be called until it returns false.
function walk(i) {
    drawBicycle(new Tortoise()
        .addTransform(Scale(scale))
                .addTransform(Translate(translateOriginX, translateOriginY))

        .addTransform(Scale(scaleDecay*i))  
        .addTransform(Rotate(rotationStep*i))
        .addTransform(Translate(translateX*i, translateY*i)),
        0,  0, "test");
    
    return i < steps;
}



function drawBicycle(t, x, y, str) {
    t = t.addTransform(Translate(x, y));
    t.jmp(-15,-5); t.circle(10);
    // t.jmp( 15,-5); t.circle(10);
    // t.jmp(-15, 5); 
    //     t.goto(0, 5); t.goto(10, -10); 
    //     t.goto(-5, -10); t.goto(-15, 5);
    // t.jmp( 0, 5); t.goto(-6, -13); t.goto(-8, -13); t.goto(-2, -13);  
    // t.jmp( 15, 5); t.goto( 9, -13);  t.goto( 14, -13); t.circle(2, 180);
    t.setheading(0);
}


////////////////////////////////////////////////////////////////
// Tortoise utility code. Created by Reinder Nijhoff 2019
// https://turtletoy.net/turtle/102cbd7c4d
////////////////////////////////////////////////////////////////

function Tortoise(x, y) {
    class Tortoise extends Turtle {
        constructor(x, y) {
            super(x, y);
            this.ps = Array.isArray(x) ? [...x] : [x || 0, y || 0];
            this.transforms = [];
        }
        addTransform(t) {
            this.transforms.push(t);
            this.jump(this.ps);
            return this;
        }
        applyTransforms(p) {
            if (!this.transforms) return p;
            let pt = [...p];
            this.transforms.map(t => { pt = t(pt); });
            return pt;
        }
        goto(x, y) {
            const p = Array.isArray(x) ? [...x] : [x, y];
            const pt = this.applyTransforms(p);
            if (this.isdown() && (this.pt[0]-pt[0])**2 + (this.pt[1]-pt[1])**2 > 4) {
               this.goto((this.ps[0]+p[0])/2, (this.ps[1]+p[1])/2);
               this.goto(p);
            } else {
                super.goto(pt);
                this.ps = p;
                this.pt = pt;
            }
        }
        position() { return this.ps; }
    }
    return new Tortoise(x,y);
}