Skip to content

Instantly share code, notes, and snippets.

@realfun
Last active May 8, 2024 03:33
Show Gist options
  • Save realfun/90947bfd06f3e5cef49dda4ac69f536a to your computer and use it in GitHub Desktop.
Save realfun/90947bfd06f3e5cef49dda4ac69f536a to your computer and use it in GitHub Desktop.
Python Turtle Spirograph - by chatgpt; steps calculation is interesting
# Set up the screen
screen = turtle.Screen()
screen.bgcolor("white")
# Set up the turtle
pen = turtle.Turtle()
pen.speed(0) # Fastest drawing speed
pen.width(2)
def draw_spirograph(R, r, d):
"""
Draw a spirograph using:
R: Radius of the fixed outer circle.
r: Radius of the rolling circle.
d: Distance from the center of the rolling circle to the drawing point.
"""
# Calculate the number of steps based on the greatest common divisor of R and r
gcd_val = math.gcd(R, r)
num_steps = int(r/gcd_val) * 360
for i in range(0, num_steps, 30):
theta = math.radians(i)
x = (R - r) * math.cos(theta) + d * math.cos(((R - r) / r) * theta)
y = (R - r) * math.sin(theta) - d * math.sin(((R - r) / r) * theta)
pen.goto(x, y)
pen.hideturtle()
# Parameters for the spirograph
R = 100 # Radius of the fixed circle
r = 87 # Radius of the rolling circle
d = 100 # Distance from the center of the rolling circle to the drawing point
pen.up()
pen.goto((R-r)+d, 0) # Move the pen to the starting point
pen.down()
draw_spirograph(R, r, d)
# Finish up
turtle.done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment