Pine Tree
Back to index
Polygons can be useful, but they can be quite complex if they contain enough
vertices. Study the following program. Get it up and running and run it
several times.
#!/usr/bin/python
from Tkinter import *
import random
master = Tk()
master.wm_title("RANDOM POLYGON")
w = Canvas(master, width=400, height=400, bg="black")
w.pack()
pts = []
for x in range(0,50):
pts.append(random.randint(10,390))
w.create_polygon(pts,width=2,outline="green")
print pts
mainloop()
You will notice that this program produces a different polygon each time you
run it. As you no doubt have figured out, this is because the coordinates of
the vertices are generated in a random manner. The create_polygon function reads the points
in pairs from the list supplied to it, FYI.
ASSIGNMENT:
Basically all you have to do is recreate the "pine tree" shown to the right.
Instead of randomly generating a list of coordinates, you will need to
carefully plan your list of coordinates. It is recommended that the student
draw this figure out on paper and then plot the points. Finally, simply
enter the points into a list and all should go well. Hopefully!
HINT: Obviously you could begin your list at any vertex on the tree, but
there is one vertex which seems like the most natural to begin at. Of
course, you are free to begin your list wherever you see fit.
|
|
|