Bondspencil
Bondspencil

Reputation: 93

Repeating functions with python turtle

I wish to know way to make loop of function to recreate the same shape/pattern(google photo logo) with a different rotation and position and different variables such as color. Below is code that will allow me to make one of the pallets with the correct angles but ratio is not exact. Also prefer not use any goto/home function as I need repeat this drawing later. Should I have used left/right for direction instead of set heading?

def photo():
    speed(1) # turtle speed (debugging)
    #speed(0)
    length = 50

    penup()
    color("#4688f4") #Blue petal
    begin_fill() 
    setheading(25)
    forward(length/5.5)
    setheading(0)
    forward(length)
    setheading(227)
    forward(length*0.87)
    setheading(135)
    forward(length*0.8)
    end_fill()

    color("#3d6ec9") #Blue petal
    begin_fill() 
    setheading(250)
    forward(length/5)
    setheading(270)
    forward(length/2.6)
    setheading(0)
    forward(length/1.6)
    end_fill() 

here you see the drawing from the code...

thats the above described turtle drawing

Update:

enter image description here

Upvotes: 3

Views: 2898

Answers (2)

cdlane
cdlane

Reputation: 41872

Why is there like weird gap on blue petal while others don't?

To draw this cleanly, we need some sort of geometric model. The one I'll use is a matching pair of right triangles with a base of 7 units and 45 degree angles:

enter image description here

I've put a red dot at what I consider the logical origin of our drawing. To keep the math consistent, we'll cut the image we want out of the above figure:

enter image description here

Should I have used left/right for direction instead of set heading?

The code to draw this figure and rotate it can't use setheading() as that's absolute and we have to draw relative to our logical origin:

from turtle import *

UNIT = 50

def photo(petal, shadow):

    right(45)  # move from "origin" to start of image
    forward(0.45 * UNIT)
    left(70)

    color(petal)

    begin_fill()
    forward(0.752 * UNIT)
    right(25)
    forward(6 * UNIT)
    right(135)
    forward(4.95 * UNIT)
    end_fill()

    right(45)

    color(shadow)

    begin_fill()
    forward(3.5 * UNIT)
    right(90)
    forward(2.5 * UNIT)
    right(25)
    forward(0.752 * UNIT)
    end_fill()

    left(70)  # return to "origin" where we started 
    forward(0.45 * UNIT)
    right(135)

penup()

for _ in range(4):
    photo("#4688f4", "#3d6ec9")
    left(90)

hideturtle()
mainloop()

enter image description here

I'll leave the coloring issue to you.

Upvotes: 2

Cut7er
Cut7er

Reputation: 1237

very simplified answer:

my_colors =['blue', 'yellow', 'red', 'green'] # replace with hex values

for i in range(4):

    photo(my_colors[i])
    right(90)

photo function then needs to be adjusted to take a keyword, which could look like: def photo(my_color):, and where you use colors in your function, you just call it color(my_color)

but of course you need to think about where you will turn after each loop and if you will need to move forward as well.

Upvotes: 3

Related Questions