Project 4 Tricks

412 days ago by tazzalenghe

# this functions plots an m by m "chessboard" with squares that are of size n by n # using the colors b and w for alternate squares # if s=1 we save the figure otherwise we show it def chessboard(m,n,b,w,s): # start with an empty list to hold the plot board = plot([]) # variables i and j index the squares of the board # variables k and l give the coordinates of the bottom left corners of the squares # if m=20 and n=25, k and l would be the points between 0 and 500 with coordinates divisible by 25 # what m and n do you use to get points between 0 and 500 with coordinates divisible by 25? for i in range(m): k = n*i for j in range(m): l = n*j # now I plot the (i,j)th square # this will illustrate how to very the color based on a test # and to assemble them into a single plot # in your project you would use points instead of polygons (and control the size) # and your color depends on extinction rather than alternating # a square b if (i+j) is even, w if it is odd if ((i+j) % 2) == 0: # % computes integer remainders c = b else: c = w # first we make a list of the corners of the (i,j)th square square = [[k,l], [k+n,l], [k+n,l+n], [k,l+n]] # convert this to a plotted square with color and add to the board board += polygon(square, color=c) # draw the board with same x and y scale (aspect_ratio) and mn dots to the inch (dpi) to scale a bit if s: board.save("board_"+ str(m)+"_"+str(n)+".pdf", aspect_ratio=1, axes=false, dpi=m*n) else: board.show(axes=false, aspect_ratio=1, dpi=m*n) 
       
# show a standard board chessboard(8,10, "black", "lightgray", 0) 
       
# show and save a nonstandard board: click on the file name to open the pdf chessboard(30,2, "red", "blue", 0) chessboard(30,2, "red", "blue", 1) 
       
# saving lets you get a larger scale view of the plot than you see in the notebook # this example shows why you might want this : warning it takes a bit of time to run chessboard(100, 5, "yellow", "green", 0) chessboard(100, 5, "yellow", "green", 1)