import math def calculate_distance(p1, p2): """ """ return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2) def to_int_points(points): ints=[] for pt in points: #print(pt) value = [int(pt[0]),int(pt[1])] #print(value) ints.append(value) return ints debug = False def divide_line_to_points(points,divided): # return divided + 1 total_length = 0 line_length_list = [] for i in range(len(points)-1): pt_length = calculate_distance(points[i],points[i+1]) total_length += pt_length line_length_list.append(pt_length) splited_length = total_length/divided def get_new_point(index,lerp): pt1 = points[index] pt2 = points[index+1] diff = [pt2[0] - pt1[0], pt2[1]-pt1[1]] new_point = [pt1[0]+diff[0]*lerp,pt1[1]+diff[1]*lerp] if debug: print(f"pt1 ={pt1} pt2 ={pt2} diff={diff} new_point={new_point}") return new_point if debug: print(f"{total_length} splitted = {splited_length} line-length-list = {len(line_length_list)}") splited_points=[points[0]] for i in range(1,divided): need_length = splited_length*i if debug: print(f"{i} need length = {need_length}") current_length = 0 for j in range(len(line_length_list)): line_length = line_length_list[j] current_length+=line_length if current_length>need_length: if debug: print(f"over need length index = {j} current={current_length}") diff = current_length - need_length lerp_point = 1.0 - (diff/line_length) if debug: print(f"over = {diff} lerp ={lerp_point}") new_point = get_new_point(j,lerp_point) splited_points.append(new_point) break splited_points.append(points[-1]) # last one splited_points=to_int_points(splited_points) if debug: print(f"sp={len(splited_points)}") return splited_points def points_to_bbox(points): x1=float('inf') x2=0 y1=float('inf') y2=0 for point in points: if point[0]x2: x2=point[0] if point[1]y2: y2=point[1] return [x1,y1,x2-x1,y2-y1] def expand_bbox(bbox,left=5,top=5,right=5,bottom=5): left_pixel = bbox[2]*(float(left)/100) top_pixel = bbox[3]*(float(top)/100) right_pixel = bbox[2]*(float(right)/100) bottom_pixel = bbox[3]*(float(bottom)/100) new_box = list(bbox) new_box[0] -=left_pixel new_box[1] -=top_pixel new_box[2] +=left_pixel+right_pixel new_box[3] +=top_pixel+bottom_pixel return new_box