import random # The lists are such that the i-th elements are the parameters # of the i-th rectangle, where i ranges from 0 to len(bottomX)-1. # So bottomX[i] and bottomY[i] are the coordinates of the # rectangle in position i of the lists. And height[i], width[i] are # its height and width. bottomX = [0] bottomY = [0] height = [10] width = [10] def remove_rect(k): # To remove the k-th rectangle, use the pop() function # in a list. We need to remove the k-th element in each # of the four lists. bottomX.pop(k) bottomY.pop(k) width.pop(k) height.pop(k) def random_vertical(k): x = random.uniform(bottomX[k], bottomX[k] + width[k]) return x def random_horizontal(k): y = random.uniform(bottomY[k], bottomY[k] + height[k]) return y def split_vertical(k, x): # We want to replace the k-th rectangle with two new # ones added at the end, a left one and a right one (in that order). # The split occurs at x # First the left one. bottomX.append(bottomX[k]) bottomY.append(bottomY[k]) width.append(x - bottomX[k]) height.append(height[k]) # Next, the right one bottomX.append(x) bottomY.append(bottomY[k]) width.append(bottomX[k] + width[k] - x) height.append(height[k]) # Remove k-th rectangle remove_rect(k) def split_horizontal(k, y): # Write your code here to perform a horizontal split # using the horizontal line that goes through the value y # Add two rectangles in this order: first the bottom one, # then the top one. # First, write code for making the bottom one: # Next, write code for making the top one: # Remove k-th rectangle remove_rect(k) def split_both(k, x, y): # x splits vertically, y splits horizontally. # Write your code here to add four new rectangles in # the following order: bottom-left, top-left, top-right, bottom-right. # Make and add bottom left rectangle: # Make and add top left rectangle: # Make and add top right rectangle: # Make and add bottom right rectangle: # Remove k-th rectangle remove_rect(k) def rect_str(k): s = '[Rectangle: k={:d} x={:.2f} y={:.2f} w={:.2f} h={:.2f}]'.format(k,bottomX[k],bottomY[k],width[k],height[k]) return s def print_nice(k): print(rect_str(k))