Spirograph 003

Published under the MIT Licence

Log in to post a comment.

// TurtleToy Spirograph Script
// Initial spirograph from Chat GPT
// You can find the Turtle API reference here: https://turtletoy.net/syntax
// Released under the MIT licence 
// https://mit-license.org/
// you can use this for commercial gain if you like eg you can sell artworks with this code.

turtle = new Turtle
var step = 0

// Adjust these values to change the appearance of the spirograph
const R = 85;  // min=1 max=100 step=1 // Radius of fixed circle
const rMoving = 67.8;   // min=1 max=100 step=0.1 // Radius of moving circle
const Scale = 1;   // min=0.2 max=5 step=0.1
const Steps = 238;   // min=1 max=1000 step=1 
const s = 38 // min=-100 max=100 step=1

function spirograph(R, r, Scale) {
    for (let t = 0; t < Steps; t += 0.1) {
    
        // Parametric equations for spirograph
         x = Scale * ((R - rMoving) * Math.cos(t) + s * Math.cos((R - rMoving) * t / r));
         y = Scale * ((R - rMoving) * Math.sin(t) - s * Math.sin((R - rMoving) * t / r));
        // Move the turtle to the calculated point
        turtle.goto(x, y);
        turtle.pendown();
    }
}

turtle.penup();
spirograph(R, rMoving, Scale);