Spirograph 002

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 = 100;  // min=1 max=100 step=1 // Radius of fixed circle
const rMoving = 60;   // min=1 max=100 step=0.1 // Radius of moving circle
const Scale = 50;   // min=1 max=100 step=1 // Offset from the center of the moving circle
const Steps = 365;   // min=1 max=1000 step=1 // Offset from the center of the moving circle

// const grid = 12; // min=1 max=24 step=1

function spirograph(R, r, Scale) {
    for (let t = 0; t < Steps; t += 0.01) {
    
        // Parametric equations for spirograph
         x = (R - rMoving) * Math.cos(t) + Scale * Math.cos((R - rMoving) * t / r);
         y = (R - rMoving) * Math.sin(t) - Scale * 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);