Spirograph Turtle Python

Singh2013 picture Singh2013 · Sep 21, 2013 · Viewed 7.3k times · Source

Does anybody here knows how to play with turtle and has an idea how to use turtle?

I have trouble getting the thing to work as the picture shown below. [Ignore the colors]

Image

from turtle import *
from math import *


def formulaX(R, r, p, t):
    x = (R-r)*cos(t) - (r+p)*cos((R-r)/r*t)

def formulaY(R, r, p, t):
    y = (R-r)*sin(t) - (r+p)*sin((R-r)/r*t)

def t_iterating(R, r, p):
    t = 2*pi
    up()
    goto(formulaX, formulaY)
    down()

    while (True):
        t = t+0.01
        formulaX(R, r, p, t)
        formulaY(R, r, p, t)


def main():
    R = int(input("The radius of the fixed circle: "))
    r = int(input("The radius of the moving circle: "))
    p = int(input("The offset of the pen point, between <10 - 100>: "))

    if p < 10 or p > 100:
        input("Incorrect value for p!")

    t_iterating(R, r, p)

    input("Hit enter to close...")

main()'

I am trying to make that kind of shape. Here is the coding I have done so far. I am bad at python.

Thank you. I need help. Thanks!

Answer

Brionius picture Brionius · Sep 21, 2013

Try changing your t_iterating function to this:

def t_iterating(R, r, p):
    t = 2*pi          # It seems odd to me to start from 2*pi rather than 0.
    down()

    while t < 20*pi:  # This loops while t goes from 2*pi to 20*pi.
        t = t+0.01
        goto(formulaX(R, r, p, t), formulaY(R, r, p, t))
    up()