I am trying to draw this pattern using the turtle module:
Not only this one, but I am also having trouble when applying loops to draw any kind of patterns (especially when it comes to patterns with different shapes lying inside each other, I just simply do not know where to start and how to organize the functions to draw those kinds of complex patterns like that). Therefore, I usually go step by step knowing that there should be a loop to make the turtle do the same thing all over again.
Here is what I have so far:
import turtle
wn = turtle.Screen()
t = turtle.Turtle()
def pattern(t,clr):
t.screen.bgcolor("white")
t.pensize(10)
for i in range(2):
t.pd()
t.fd(100) ##
t.rt(90)
t.fd(100) ##
t.rt(90)
t.fd(90) ##
t.rt(90)
t.fd(75) ##
t.rt(90)
t.fd(70) ##
t.rt(90)
t.fd(38) ##
t.rt(90)
t.fd(38) ##
t.pu()
t.lt(90)
t.fd(77)
t.pd()
t.lt(90)
t.fd(38)
t.rt(90)
t.fd(38)
t.rt(90)
t.fd(70)
t.rt(90)
t.fd(75)
t.rt(90)
t.fd(90)
t.rt(90)
t.fd(100)
t.rt(90)
t.fd(100)
pattern(t,"black")
Any tips and advices would be much appreciated!
Looking at the original image, and your program's output, you seem to focus on black lines on a white background, not noticing that this can be seen as white lines on a black background. That is, the width of pen strokes matches the width of the gaps between strokes. Let's start by simply overlaying a grid on the image:
Now we can describe the image in terms of grid blocks, using a grid block of arbitrary size. Here's your program rewritten in terms of grid blocks:
from turtle import Screen, Turtle
BLOCK = 15
def pattern(turtle):
turtle.pensize(BLOCK)
for _ in range(2):
turtle.pd()
turtle.fd(BLOCK * 6)
turtle.rt(90)
turtle.fd(BLOCK * 6)
turtle.rt(90)
turtle.fd(BLOCK * 5)
turtle.rt(90)
turtle.fd(BLOCK * 4)
turtle.rt(90)
turtle.fd(BLOCK * 3)
turtle.rt(90)
turtle.fd(BLOCK * 2)
turtle.rt(90)
turtle.fd(BLOCK * 1)
turtle.pu()
turtle.lt(90)
turtle.fd(BLOCK * 4)
turtle.lt(90)
turtle.pd()
turtle.fd(BLOCK * 1)
turtle.rt(90)
turtle.fd(BLOCK * 2)
turtle.rt(90)
turtle.fd(BLOCK * 3)
turtle.rt(90)
turtle.fd(BLOCK * 4)
turtle.rt(90)
turtle.fd(BLOCK * 5)
turtle.rt(90)
turtle.fd(BLOCK * 6)
turtle.rt(90)
turtle.fd(BLOCK * 6)
turtle = Turtle()
pattern(turtle)
screen = Screen()
screen.exitonclick()
Looking at this modified code and image, do you get a sense of where you might be able to consolidate code by using a loop?