Cobwebs
Back to index
Turtle Graphics was somewhat famous within educational circles back in the 1980s and 1990s for being useful for generating
interesting patterns. The cobweb design was a favorite. Try out this sample program to see how this works.
#!/usr/bin/python
import turtle
t=turtle.Pen()
t.speed(3)
t.screen.bgcolor("blue")
t.pencolor("yellow")
t.pensize=2
t.shape("turtle")
for gap in range(0,201,20):
t.penup()
t.setpos(-200,gap)
t.pendown()
t.setpos(-200+gap,200)
t.penup()
t.setpos(200,gap)
t.pendown()
t.setpos(200-gap,200)
t.penup()
t.setpos(0,gap)
t.pendown()
t.setpos(200-gap,0)
t.ht()
turtle.mainloop()
This example program is pretty easy to understand. The only aspect of the
program that could
be slightly tricky is understanding the strategy behind the use of the gap variable. A
simpler way of writing this program would be to create three for-loops, one
for each section of cobweb. However, that would require more lines of code.
There's even a way to use embedded for-loops to generate each section of
cobweb, but it requires more lines of code also.
Often longer programs are easier to understand than well-planned programs
which use a minimal number of lines.
Admittedly, all the penup and pendown commands required in this program are
a little tedious.
ASSIGNMENT:
The sample program generates three sections of cobweb. Your job will be to
generate the other five sections. The five sections include the lower two
corners and the three inside sections. The inside sections look like a
curved, four-pointed star when all drawn together.
|