Untitled
unknown
python
a year ago
2.4 kB
1
Indexable
Never
import numpy as np def read_input(dim, length): values = [] for i in range(length): values.append([]) for i in range(length): coord = input().split("#") coord = coord[1].split() for j in range(dim): values[i].append(np.longdouble(coord[j])) return values def print_output(values: list): length = len(values) max_value = max(values, key=lambda coordinate: [coordinate[0], coordinate[1]]) # Reverse list so CH is clockwise values = list(reversed(values)) # Move the last value from the end to the beginning, # until the max value is the first value: while values[0] != max_value: values.insert(0, values.pop()) # Print point of CH print(length) for i in range(length): if values[i][0] == int(values[i][0]) and values[i][1] == int(values[i][1]) : print(int(values[i][0]), int(values[i][1])) else: print(f"{np.round(values[i][0], 3):.3f}", f"{np.round(values[i][1], 3):.3f}") def ch_preparatahong(points): def orientation(p, q, r): '''Returns positive value if p-q-r are clockwise, neg if ccw, zero if collinear''' return (q[1]-p[1])*(r[0]-p[0]) - (q[0]-p[0])*(r[1]-p[1]) def find_tangent(pt, direction): '''Finds the upper/lower tangent going in the specified direction. `direction` must be either +1 or -1.''' t_pt = pt while True: prev_pt = t_pt for i in range(len(points)): o = orientation(pt, t_pt, points[i]) if o == direction: t_pt = points[i] if prev_pt == t_pt: break return t_pt # sort the points in lexicographic order points = sorted(points) # Find the upper and lower tangent for the leftmost and rightmost points l = find_tangent(points[0], 1) r = find_tangent(points[-1], -1) # compute the hull using the upper/lower tangent hull = [l] i = points.index(l) while points[i] != r: hull.append(points[i]) i = (i + 1) % len(points) hull.append(r) return hull initInput = input().split() dim = int(initInput[0]) n = int(initInput[1]) data = read_input(dim, n) print_output(ch_preparatahong(data))