【发布时间】:2016-06-21 14:09:36
【问题描述】:
我想为RDP algorithm修改以下python脚本,目的是不使用epsilon,而是选择我想在决赛中保留的点数:
class DPAlgorithm():
def distance(self, a, b):
return sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
def point_line_distance(self, point, start, end):
if (start == end):
return self.distance(point, start)
else:
n = abs(
(end[0] - start[0]) * (start[1] - point[1]) - (start[0] - point[0]) * (end[1] - start[1])
)
d = sqrt(
(end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2
)
return n / d
def rdp(self, points, epsilon):
"""
Reduces a series of points to a simplified version that loses detail, but
maintains the general shape of the series.
"""
dmax = 0.0
index = 0
i=1
for i in range(1, len(points) - 1):
d = self.point_line_distance(points[i], points[0], points[-1])
if d > dmax :
index = i
dmax = d
if dmax >= epsilon :
results = self.rdp(points[:index+1], epsilon)[:-1] + self.rdp(points[index:], epsilon)
else:
results = [points[0], points[-1]]
return results
我发现了一个具有这种精神的 Java 脚本:https://gist.github.com/msbarry/9152218
有人知道 Python 3.X 的版本吗?
谢谢 妈咪
【问题讨论】:
标签: python algorithm computational-geometry simplification