Back to index
#!/usr/bin/python from Tkinter import * master = Tk() master.wm_title("SQUARRAL") w = Canvas(master, width=400, height=400, bg="black") w.pack() leng=180 pts = [200,10,390,10] px=390 py=10 for n in range(0,9): #side one nx=px ny=py+leng pts.append(nx) pts.append(ny) leng=leng-5 px=nx py=ny #side two nx=px-leng ny=py pts.append(nx) pts.append(ny) leng=leng-5 px=nx py=ny #side three nx=px ny=py-leng pts.append(nx) pts.append(ny) py=ny leng=leng-5 #side four nx=px+leng ny=py pts.append(nx) pts.append(ny) px=nx py=ny leng=leng-5
# reverse list and append: xvals=[] for n in pts[::2]: xvals.append(n) yvals=[] for n in pts[1::2]: yvals.append(n) xvals.reverse() yvals.reverse() revPTS=[] for n2 in range(0,len(xvals)): revPTS.append(xvals[n2]) revPTS.append(yvals[n2]) for n3 in revPTS: pts.append(n3) # draw polygon w.create_polygon(pts,width=2,outline="green") mainloop()
SQUARRAL

Get the code displayed to the left and above up and running. It could be condensed, but doing so might obfuscate it somewhat. So, for the sake of clarity it is shown as is. Even at that it may be difficult to follow.

Basically the entire program is focused on filling the list called pts with the right bunch of numbers to create a squarral. Keep in mind that these numbers function as x-y-coordinate pairs.

As an experiment delete the lines between "#reverse list and append" and "draw polygon". Notice that the squarral still gets drawn, but that an unwanted line appears from the inside of the squarral to the beginning of the squarral. In order to get rid of that line it is necessary to retrace all the coordinate pairs from the end to the beginning. The create_polygon method closes the polygon by connecting the last point to the first point. If the two points are the same, then all is good in the case of this sample program.


ASSIGNMENT: You will create four squarrals symmetrically arranged on the canvas. Keep the one for the upper-right quadrant as is, but create three more for the other three quadrants. Keep in mind that some of your squarrals will rotate clockwise and others will go counter-clockwise.

NOTE: Strategically placed print lines would allow you to monitor the contents of the lists called pts and revPTS.