[pyhton]拉伸矩形 stretch rectangle
拉伸矩形:
import pandas as pd import matplotlib.patches as mpathes from matplotlib import pyplot as plt def stretch_bb(bb, shift_x, shift_y, free_points=None): """ p2----------p3 | | | | p1----------p4 :param bb: :param shift_x: :param shift_y: :param free_points: :return: """ minx, miny, maxx, maxy = bb p1 = (minx, miny) p2 = (minx, maxy) p3 = (maxx, maxy) p4 = (maxx, miny) new_point_lst = [] for free, point in zip(free_points, (p1, p2, p3, p4)): if free: point = point[0] + shift_x, point[1] + shift_y new_point_lst.append(point) minx = min([i[0] for i in new_point_lst]) miny = min([i[1] for i in new_point_lst]) maxx = max([i[0] for i in new_point_lst]) maxy = max([i[1] for i in new_point_lst]) return minx, miny, maxx, maxy def stretch_data_lst(data_lst, base_point, shift_lst): shift_l, shift_t, shift_r, shift_b = shift_lst BaseX = base_point[0] BaseY = base_point[1] new_bb_lst = [] for data in data_lst: x0, y0, x1, y1 = data if shift_l: free0 = free1 = free2 = free3 = False if x0 < BaseX: free0 = free1 = True if x1 < BaseX: free2 = free3 = True free_points = free0, free1, free2, free3 x0, y0, x1, y1 = stretch_bb(bb=(x0, y0, x1, y1), shift_x=shift_l, shift_y=0, free_points=free_points) if shift_t: free0 = free1 = free2 = free3 = False if y1 > BaseY: free1 = free2 = True if y0 > BaseY: free0 = free3 = True free_points = free0, free1, free2, free3 x0, y0, x1, y1 = stretch_bb(bb=(x0, y0, x1, y1), shift_x=0, shift_y=shift_t, free_points=free_points) if shift_r: free0 = free1 = free2 = free3 = False if x0 > BaseX: free0 = free1 = True if x1 > BaseX: free2 = free3 = True free_points = free0, free1, free2, free3 x0, y0, x1, y1 = stretch_bb(bb=(x0, y0, x1, y1), shift_x=shift_r, shift_y=0, free_points=free_points) if shift_b: free0 = free1 = free2 = free3 = False if y0 < BaseY: free0 = free3 = True if y1 < BaseY: free1 = free2 = True free_points = free0, free1, free2, free3 x0, y0, x1, y1 = stretch_bb(bb=(x0, y0, x1, y1), shift_x=0, shift_y=shift_b, free_points=free_points) new_bb_lst.append((x0, y0, x1, y1)) return new_bb_lst def get_bb_lst_center(bb_lst:list): minx, miny, maxx, maxy = float('inf'), float('inf'), float('-inf'), float('-inf') for bb in bb_lst: x0, y0, x1, y1 = bb minx = min(minx, x0) miny = min(miny, y0) maxx = max(maxx, x1) maxy = max(maxy, y1) return (minx+maxx)/2, (miny+maxy)/2 bb_lst = [(102660, 52530, 122080, 53530), (102660, 52530, 103660, 76040), (121080, 52530, 122080, 76040), (102660, 75040, 122080, 76040), (102660, 52530, 122080, 53530), (102660, 52530, 103660, 76040), (121080, 52530, 122080, 76040), (102660, 75040, 122080, 76040), (121470, 53835, 121690, 54055), (121470, 54305, 121690, 54525), (121470, 54775, 121690, 54995), (121470, 55245, 121690, 55465), (121470, 55715, 121690, 55935), (121470, 56185, 121690, 56405), (121470, 56655, 121690, 56875), (121470, 57125, 121690, 57345), (121470, 57595, 121690, 57815), (121470, 58065, 121690, 58285), (121470, 58535, 121690, 58755), (121470, 59005, 121690, 59225), (121470, 59475, 121690, 59695), (121470, 59945, 121690, 60165), (121470, 60415, 121690, 60635), (121470, 60885, 121690, 61105), (121470, 61355, 121690, 61575), (121470, 61825, 121690, 62045), (121470, 62295, 121690, 62515), (121470, 62765, 121690, 62985), (121470, 63235, 121690, 63455), (121470, 63705, 121690, 63925), (121470, 64175, 121690, 64395), (121470, 64645, 121690, 64865), (121470, 65115, 121690, 65335), (121470, 65585, 121690, 65805), (121470, 66055, 121690, 66275), (121470, 66525, 121690, 66745), (121470, 66995, 121690, 67215), (121470, 67465, 121690, 67685), (121470, 67935, 121690, 68155), (121470, 68405, 121690, 68625), (121470, 68875, 121690, 69095), (121470, 69345, 121690, 69565), (121470, 69815, 121690, 70035), (121470, 70285, 121690, 70505), (121470, 70755, 121690, 70975), (121470, 71225, 121690, 71445), (121470, 71695, 121690, 71915), (121470, 72165, 121690, 72385), (121470, 72635, 121690, 72855), (121470, 73105, 121690, 73325), (121470, 73575, 121690, 73795), (121470, 74045, 121690, 74265), (121470, 74515, 121690, 74735), (103800, 75430, 104020, 75650), (104270, 75430, 104490, 75650), (104740, 75430, 104960, 75650), (105210, 75430, 105430, 75650), (105680, 75430, 105900, 75650), (106150, 75430, 106370, 75650), (106620, 75430, 106840, 75650), (107090, 75430, 107310, 75650), (107560, 75430, 107780, 75650), (108030, 75430, 108250, 75650), (108500, 75430, 108720, 75650), (108970, 75430, 109190, 75650), (109440, 75430, 109660, 75650), (109910, 75430, 110130, 75650), (110380, 75430, 110600, 75650), (110850, 75430, 111070, 75650), (111320, 75430, 111540, 75650), (111790, 75430, 112010, 75650), (112260, 75430, 112480, 75650), (112730, 75430, 112950, 75650), (113200, 75430, 113420, 75650), (113670, 75430, 113890, 75650), (114140, 75430, 114360, 75650), (114610, 75430, 114830, 75650), (115080, 75430, 115300, 75650), (115550, 75430, 115770, 75650), (116020, 75430, 116240, 75650), (116490, 75430, 116710, 75650), (116960, 75430, 117180, 75650), (117430, 75430, 117650, 75650), (117900, 75430, 118120, 75650), (118370, 75430, 118590, 75650), (118840, 75430, 119060, 75650), (119310, 75430, 119530, 75650), (119780, 75430, 120000, 75650), (120250, 75430, 120470, 75650), (120720, 75430, 120940, 75650), (103050, 53755, 103270, 53975), (103050, 54225, 103270, 54445), (103050, 54695, 103270, 54915), (103050, 55165, 103270, 55385), (103050, 55635, 103270, 55855), (103050, 56105, 103270, 56325), (103050, 56575, 103270, 56795), (103050, 57045, 103270, 57265), (103050, 57515, 103270, 57735)] shift_lst = (0, 2500, 0, 0) fig, ax = plt.subplots() base_center = get_bb_lst_center(bb_lst) for bb in bb_lst: min_x, min_y, max_x, max_y = bb rect = mpathes.Rectangle((min_x, min_y), max_x - min_x, max_y - min_y, fill="", color='r') ax.add_patch(rect) new_bb_lst = stretch_data_lst(bb_lst, base_center, shift_lst) for bb in new_bb_lst: min_x, min_y, max_x, max_y = bb rect = mpathes.Rectangle((min_x, min_y), max_x - min_x, max_y - min_y, fill="", color='blue') ax.add_patch(rect) plt.text(*base_center, s="center") plt.axis('equal') plt.show()
效果: