这是我使用的纯Python O(n)实现:
import math
"""
Minimal Enclosing Parallelogram
area, v1, v2, v3, v4 = mep(convex_polygon)
convex_polygon - array of points. Each point is a array [x, y] (1d array of 2 elements)
points should be presented in clockwise order.
the algorithm used is described in the following paper:
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.53.9659&rep=rep1&type=pdf
"""
def distance(p1, p2, p):
return abs(((p2[1]-p1[1])*p[0] - (p2[0]-p1[0])*p[1] + p2[0]*p1[1] - p2[1]*p1[0]) /
math.sqrt((p2[1]-p1[1])**2 + (p2[0]-p1[0])**2))
def antipodal_pairs(convex_polygon):
l = []
n = len(convex_polygon)
p1, p2 = convex_polygon[0], convex_polygon[1]
t, d_max = None, 0
for p in range(1, n):
d = dista