3

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

10
  • So.... youre just asking how a loop looks like? Commented Aug 30, 2018 at 12:56
  • you will need to put the above function into a loop, which will be done 4 times - after each loop, you will need to adjust your position. color-changes can be given to your function as parameters from a list. Before you go too deep into the coding, try thinking about where you land after each loop and where you have to go in order to start the next one! Commented Aug 30, 2018 at 12:59
  • How should I implemented this as the petal are rotated, do make these angle a variable and just add 90 degrees and for the starting position just move it desire location and basically paste what is above? Commented Aug 30, 2018 at 13:00
  • 1
    wrap the function call in a loop and add turtle.tilt(angle) at the end of your function definition to rotate the turtle after each drawing. replace angle with appropriate value Commented Aug 30, 2018 at 13:03
  • 1
    @FlyingThunder, I don't believe your suggestion will work. tilt(angle) tilts the turtle image, not it's direction, not the thing it's drawing. A right(90) will likely work better. Commented Aug 30, 2018 at 16:27

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

You need to re-arange the list though, as in the picture you want to recreate blue+yellow / red+green are not adjacent. Critical Error!!1
:-D just picked 4 random colors but thanks, critical error dodged!
I will look more into it but I figure a quick and dirty way of doing it but won't be as clean as using loops. I will do some more test!
Why is there like weird gap on blue petal while others don't? They basically have same code but alter angles. i.sstatic.net/rOP8F.png
no idea! maybe because you calculate with weird numbers like length*0.87 - this may be a cause for errors because of rounding
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.