Spirograph
Spirograph Generator
Log in to post a comment.
// TurtleToy: Spirograph
// User-defined parameters
const R = 96;// min=1 max=100 step=1 // Radius of the stator
const B = 13;// min=1 max=100 step=1 // Radius of the rotor
const d = 100;// min=1 max=100 step=1 // Drawing point distance from the rotor's center
const A = 0.01;// min=0.01 max=1 step=0.01 // Angle step for plotting points (in radians)
const size = 0.5; // min=0.1 max=1 step=0.1 // Scaling factor for the overall size
// Set up the drawing
Canvas.setpenopacity(1);
const turtle = new Turtle();
// Draw the Epicycloid
for (let t = 0; t < 2 * Math.PI * R / GCD(R, B); t += A) {
const x = (R + B) * Math.cos(t) - d * Math.cos((R + B) / B * t);
const y = (R + B) * Math.sin(t) - d * Math.sin((R + B) / B * t);
if (t === 0) {
turtle.jump(x * size, y * size);
} else {
turtle.goto(x * size, y * size);
}
}
function GCD(a, b) {
// Helper function to calculate the Greatest Common Divisor
return b === 0 ? a : GCD(b, a % b);
}
function loop() {
return false; // Run the script once without looping
}