Spirograph Turtle Python

Question:

How can I play with a turtle and how can I use a turtle?

I have trouble getting the thing to work as in 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.

Asked By: Singh2013

||

Answers:

No! You’re missing the point of the turtle! You should try to do it all with relative movements of the turtle. Think about how you would draw the shape if you were the turtle, crawling on a large floor, dragging a paintbrush from your butt.

At each small fragment of time, the turtle will perform one small iteration of a differential equation which governs the whole behavior. It is not generally wise to precompute the x y coordinates and use the turtle’s GOTO function.

The turtle itself should have only relative knowledge of its surroundings. It has a direction, and a position. And these two pieces of state are modified by turning and moving.

So, think about how you would draw the spiral. Particularly, think about drawing the very first circle. As the circle appears to close, something interesting happens: it misses. It misses by a tiny little amount, which turns out to be a fraction of a circle. It is this missing curvature that closes the large pattern of circles in a circle, as they add up to one complete turn.

When the whole figure is drawn, the turtle is back to its original position and orientation.

Answered By: luser droog

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()
Answered By: Brionius

This is my code. The color may not be exact, but here it is:

from turtle import *
from random import randint

speed(10000)
for i in range(20):
    col = randint(1, 5)
    if col == 1:
        pencolor("orange")
    elif col == 2:
        pencolor("blue")
    elif col == 3:
        pencolor("green")
    elif col == 4:
        pencolor("purple")
    elif col == 5:
        pencolor("dark blue")
    circle(50)
    left(20)

This is the output:

SOrry If my grammar isn't exact.

Answered By: user11093202

You basically get the turtle to loop through the 360 degrees and you can choose two pen colours.

from turtle import Turtle, Screen


tim = Turtle()
tim.shape("turtle")
tim.color("green")

### total degrees in circle = 360
### turn left must be a divisor of 360 (1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 18, 20, 24, 30, 36, 40, 45, 60, 72, 90) NOTE: some divisors do not work as well
degrees = 360
turn_left = 12
total_circles = int(degrees / turn_left)
tim.pensize(3)
tim.speed(0)


def circle_colour1():
    ### choose your colour here:
    tim.pencolor("pink")
    tim.circle(-100)
    tim.left(turn_left)


def circle_colour2():
    ### choose your colour here:
    tim.pencolor("grey")
    tim.circle(-100)
    tim.left(turn_left)


for _ in range(0, int(total_circles / 2)):
    circle_colour1()
    circle_colour2()


screen = Screen()
screen.exitonclick()

Real basic (360°/10) is:

from turtle import Turtle as d

draw = d()
draw.speed(0)
draw.pensize(3)

for _ in range(0, 36):
    draw.circle(-100)
    draw.left(10)
Answered By: Brisco

My code is here and the function was built for automatically choosing the random colour.

from turtle import Turtle, Screen
import random

timmy = Turtle()
screen = Screen()
screen.colormode(255)
timmy.shape("turtle")
timmy.speed("fastest")
angle = [0, 90, 180, 270]


def random_color():
    red = random.randint(0, 255)
    green = random.randint(0, 255)
    blue = random.randint(0, 255)
    colour = (red, green, blue)
    return colour


def draw_circles(num_of_gap):
    for _ in range(int(360 / num_of_gap)):
        timmy.color(random_color())
        timmy.circle(100)
        timmy.right(num_of_gap)


draw_circles(20)

screen.exitonclick()
Answered By: Nick

Spirograph using Python Turtle with random colours

Code:

import random
from turtle import Turtle, Screen


tim = Turtle()
tim.shape("classic")


def turtle_color():
    R = random.random()
    G = random.random()
    B = random.random()
    return tim.pencolor(R, G, B)


tim.speed("fastest")


for _ in range(72):
    turtle_color()
    tim.circle(100)
    tim.left(5)


screen = Screen()
screen.exitonclick()

Output:

image

Answered By: user19017546

The spyrograph package uses turtle under the hood to trace these sorts of shapes (DISCLAIMER: I am the author of this library)

To install:

pip install spyrograph

Define the shape:

from spyrograph import Hypotrochoid
import numpy as np

hypotrochoid = Hypotrochoid(
    R=100, 
    r=4, 
    d=80, 
    thetas=np.arange(0, 20*np.pi, .01)
)

And then you have a few options for tracing the shape:

Draw it instantaneously:

hypotrochoid.trace(exit_on_click=True)

Circular shape traced from many lines resembling a donut with a very small hole

Show the turtle as it draws:

hypotrochoid.trace(exit_on_click=True, hide_turtle=False, frame_pause=.01)

Circular shape traced from many lines resembling a donut with a very small hole being traced on screen

Or show the spirograph circles tracing the shape:

hypotrochoid.trace(exit_on_click=True, show_circles=True, frame_pause=.01)

Circular shape traced from many lines resembling a donut with a very small hole being traced on screen by a circle rolling around the inside of another circle

Answered By: Chris Greening
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.