6

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Singh2013
  • 169
  • 1
  • 4
  • 12
  • 1
    It's always helpful in stackoverflow to explain what you mean by "it doesn't work". What does it do, and why is it not good enough? Is there an error? If so, print the error stack trace! – Brionius Sep 21 '13 at 19:43

7 Answers7

5

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()
Brionius
  • 13,858
  • 3
  • 38
  • 49
  • 1
    Okay, I did that. I changed the Down() to up() and at the bottom, I change up() to down(). SO How would I stop the loop? Thanks a bunch! – Singh2013 Sep 21 '13 at 20:13
  • You need to replace the "True" in your `while` statement with a test. I'll give you an example above. – Brionius Sep 21 '13 at 20:16
  • Oh okay, I understand now. I messed up a little. It's suppose to be t = 0 (the second line) and for the while loop, it's while t < 2*pi. Thank you so much! I wish I can rep you since you are helping me. Thank you. I marked this as a answer! – Singh2013 Sep 21 '13 at 20:21
  • You're welcome - just FYI in stackoverflow marking an answer as accepted does add to the answerer's reputation :) – Brionius Sep 21 '13 at 20:27
3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
luser droog
  • 18,988
  • 3
  • 53
  • 105
  • 2
    I agree with @luserdroog that this is not really in the spirit of turtle, but I also think it's silly to be prescriptive about how people may or may not use a programming language. – Brionius Sep 21 '13 at 19:44
  • 1
    Edited to be more *prescriptive* than *proscriptive*. – luser droog Sep 21 '13 at 19:57
  • 2
    Ah, right. My first exposure to programming was Logo (followed by QBasic), so I understand your protectiveness for the spirit of the Turtle :) – Brionius Sep 21 '13 at 20:25
  • 1
    Particularly so because I remember "not getting it". And now I know the piece that I missed, that might have opened it up for me much earlier. Really thinking about the turtle as a physical thing would've helped me a lot. If they had had a robot turtle instead of screens, I might've made the connection. :( – luser droog Sep 21 '13 at 20:34
2

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.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
0

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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brisco
  • 99
  • 9
0

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()
Nick
  • 1
  • 2
0

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

RiveN
  • 2,595
  • 11
  • 13
  • 26
0

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

Chris Greening
  • 510
  • 5
  • 14