代码:
from collections import deque class Point: def __init__(self, x, y): self.x = x self.y = y class HalfPlane: def __init__(self, a, b, c): self.a = a self.b = b self.c = c def __call__(self, p): return self.a * p.x + self.b * p.y + self.c def __lt__(self, other): if self.a != other.a: return self.a < other.a if self.b != other.b: return self.b < other.b return self.c < other.c def halfplane_intersection(halfplanes): halfplanes.sort() q = deque() for hp in halfplanes: while len(q) >= 2 and (q[-1](q[-2]) > 0 or q[-1](hp) > 0): q.pop() while len(q) >= 2 and (q[0](q[1]) > 0 or q[0](hp) > 0): q.popleft() q.append(hp) while len(q) >= 3 and q[-1](q[-2]) > 0: q.pop() while len(q) >= 3 and q[0](q[1]) > 0: q.popleft() return q