import math import random from drawtool import DrawTool dt = DrawTool() dt.set_XY_range(0,10, 0,10) # Change to rectangles2.txt and later to rectangles3.txt filename = 'rectangles1.txt' num_rectangles = 0 bottomX = [] bottomY = [] widths = [] heights = [] does_intersect = [] def read_rectangles(filename): global num_rectangles with open(filename,'r') as in_file: line = in_file.readline() n = int(line.strip()) num_rectangles = n for i in range(n): # Read each rectangle. line = in_file.readline() data = line.split() bottomX.append(float(data[0].strip())) bottomY.append(float(data[1].strip())) widths.append(float(data[2].strip())) heights.append(float(data[3].strip())) def print_rectangles(): print('Number of rectangles =', num_rectangles) for i in range(num_rectangles): print('x =', bottomX[i], ' y =', bottomY[i], ' width =', widths[i], ' height =', heights[i]) def init_intersect(): for i in range(num_rectangles): does_intersect.append(False) def in_rectangle(x, y, w, h, x2, y2): # Write code in here to determine whether the point (x2,y2) # is inside the rectangle specified by x,y,w,h. # This function should return True or False pass def intersect(x1, y1, w1, h1, x2, y2, w2, h2): # Use in_rectangle several times to determine # whether any corner of rectangle x2,y2,w2,h2 is inside # the rectangle specified by x,y,w,h. # This function should return True or False pass def check_intersections(): # Write a nested loop here to compare all pairs # of rectangles. If the i-th rectangle has an intersection # with another, set does_intersect[i] to True. pass read_rectangles(filename) print_rectangles() init_intersect() check_intersections() # Draw in two colors for i in range(num_rectangles): color = 'b' if does_intersect[i]: color = 'r' dt.set_color(color) dt.draw_rectangle(bottomX[i],bottomY[i],widths[i],heights[i]) dt.display()