#rectangles.py from random import randint from ezgraphics import GraphicsWindow def is_any_overlap(rect,rectangles): '''return True if rect overlaps any of the rectangles''' for r in rectangles: if not (r[0][0]+r[1]/2 < rect[0][0]-rect[1]/2 or\ r[0][0]-r[1]/2 > rect[0][0]+rect[1]/2 or\ r[0][1]-r[2]/2 > rect[0][1]+rect[2]/2 or\ r[0][1]+r[2]/2 < rect[0][1]-rect[2]/2): if r != rect: #not itself return True return False def is_rect_in_rect(rect,rectangles): '''return True if rect is inside any of the rectangles''' for r in rectangles: if rect[0][0]-rect[1]/2>r[0][0]-r[1]/2 and rect[0][0]+rect[1]/2r[0][1]-r[2]/2 and rect[0][1]+rect[2]/2 max_height: print("maximum dimension rectangle",max_width) else: print("maximum dimension rectangle",max_height) rectangles.sort(key=lambda t: t[1]*t[2]) print("minimum area rectangle",rectangles[0]) print("maximum area rectangle",rectangles[-1]) #print(rectangles) canvas.setOutline("yellow") num_equal_areas = 0 for i in range(len(rectangles)-1): #must be sorted in area order i_area = rectangles[i][1]*rectangles[i][2] #width*height j = i+1 while j= rj[0][0]-rj[1]/2 and\ ri[0][1]-ri[2]/2 <= rj[0][1]+rj[2]/2 and\ ri[0][1]+ri[2]/2 >= rj[0][1]-rj[2]/2: overlaps[i] += 1 max_overlaps = max(overlaps) print("max_overlaps",max_overlaps," (cyan rectangles)") #if not is_non_overlapping: if max_overlaps > 0: # at least one non-overlapping canvas.setOutline("cyan") for i in range(len(overlaps)): if overlaps[i]==max_overlaps: print(" rectangle of max overlaps",rectangles[i]) r = rectangles[i] canvas.drawRect(r[0][0]-r[1]/2, r[0][1]-r[2]/2, r[1], r[2]) if is_show_areas: canvas.drawText(r[0][0],r[0][1],str(r[1]*r[2])) # maximum #points inside any rectangle MAGENTA rectangles num_points_inside = [] i = 0 for r in rectangles: #3-tuple of: 2-tuple of x,y, width, height num_points_inside.append(0) for p in points: #2-tuple of x,y if r[0][0]-r[1]/2 <= p[0] <= r[0][0]+r[1]/2 and \ r[0][1]-r[2]/2 <= p[1] <= r[0][1]+r[2]/2: num_points_inside[i] += 1 i += 1 max_num_pts_inside = max(num_points_inside) print("max num_points_inside",max_num_pts_inside," (magenta rectangles)") canvas.setOutline("magenta") for i in range(len(num_points_inside)): if num_points_inside[i]==max_num_pts_inside: print(" rectangle of max pts",rectangles[i]) r = rectangles[i] canvas.drawRect(r[0][0]-r[1]/2, r[0][1]-r[2]/2, r[1], r[2]) if is_show_areas: canvas.drawText(r[0][0],r[0][1],str(r[1]*r[2])) #weighted centroid BROWN disc x_prod_sum = sum([r[0][0]*r[1]*r[2] for r in rectangles]) #sum has no key!!! y_prod_sum = sum([r[0][1]*r[1]*r[2] for r in rectangles]) x_w_centroid = x_prod_sum / sum_areas y_w_centroid = y_prod_sum / sum_areas print("(x_w_centroid,y_w_centroid): (%4.1f,%4.1f)"%(x_w_centroid,y_w_centroid)," (brown dot)") canvas.setColor("brown") canvas.drawOval(x_w_centroid-1,y_w_centroid-1,3,3) #centroid ORANGE disc x_sum = sum([r[0][0] for r in rectangles]) y_sum = sum([r[0][1] for r in rectangles]) x_centroid = x_sum / len(rectangles) y_centroid = y_sum / len(rectangles) print("(x_centroid,y_centroid): (%4.1f,%4.1f)"%(x_centroid,y_centroid)," (orange dot)") canvas.setColor("orange") canvas.drawOval(x_centroid-1,y_centroid-1,3,3) canvas.setFill() # draw the points last for p in points: if is_interior(p,rectangles): canvas.setOutline("red") else: canvas.setOutline("blue") canvas.drawPoint(p[0],p[1]) from math import sqrt def distance(pt1, pt2): '''return distance between two points (two 2-tuples)''' return sqrt((pt1[0]-pt2[0])**2+(pt1[1]-pt2[1])**2) print("Click points with mouse. (Click the upperleft corner blue box to Quit)") canvas.setColor("blue") canvas.drawRect(0,0,5,5) #quitting x,y = win.getMouse() #gets a point as a 2-tuple while x>5 or y>5: print(" ("+str(x)+","+str(y)+")", end=" ") canvas.setColor("blue") canvas.drawOval(x-1,y-1, 3,3) min_d = world_width*world_height for r in rectangles: #find min distance [border] or corner for corner_offset_x in [-1,1]: for corner_offset_y in [-1,1]: d = distance((x,y),(r[0][0]+corner_offset_x*(r[1]/2),r[0][1]+corner_offset_y*(r[2]/2))) if d < min_d : min_d = d min_d_rect = r min_corner_x = corner_offset_x min_corner_y = corner_offset_y canvas.drawLine(x,y, min_d_rect[0][0]+min_corner_x*(min_d_rect[1]/2), min_d_rect[0][1]+min_corner_y*(min_d_rect[2]/2)) print("bl:%4.1f"%(min_d), end=" ") canvas.setColor("green") for r in rectangles: #is inside this rectangle if r[0][0]-r[1]/2 <= x <= r[0][0]+r[1]/2 and \ r[0][1]-r[2]/2 <= y <= r[0][1]+r[2]/2: min_in_d = 10000 # pos. inf. for corner_offset_x in [-1,1]: for corner_offset_y in [-1,1]: d = distance((x,y),(r[0][0]+corner_offset_x*(r[1]/2),r[0][1]+corner_offset_y*(r[2]/2))) if d < min_in_d : min_in_d = d min_in_d_rect = r min_in_corner_x = corner_offset_x min_in_corner_y = corner_offset_y canvas.drawLine(x,y, min_in_d_rect[0][0]+min_in_corner_x*(min_in_d_rect[1]/2), min_in_d_rect[0][1]+min_in_corner_y*(min_in_d_rect[2]/2)) print("gr:%4.1f"%(min_in_d), end=" ") print() x,y = win.getMouse() #get next point inp = input("Save rectangles to a file? (y or n) [n]: ") if inp!="" and inp.lower()[0]=='y': fname = input(" Name of file? ") outfile = open(fname, mode="w") for r in rectangles: outfile.write(str(r)+"\n") outfile.close()