gpt4 book ai didi

python : Ramer-Douglas-Peucker (RDP) algorithm with number of points instead of epsilon

转载 作者:太空狗 更新时间:2023-10-29 21:52:15 26 4
gpt4 key购买 nike

我想为 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 的版本吗?

谢谢妈妈

最佳答案

将上述链接中的 JS 代码移植到 Python [2.7]:

# -*- coding: utf-8 -*-

import math
import time


def timenow():
return int(time.time() * 1000)

def sqr(x):
return x*x

def distSquared(p1, p2):
return sqr(p1[0] - p2[0]) + sqr(p1[1] - p2[1])

class Line(object):
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
self.lengthSquared = distSquared(self.p1, self.p2)

def getRatio(self, point):
segmentLength = self.lengthSquared
if segmentLength == 0:
return distSquared(point, p1);
return ((point[0] - self.p1[0]) * (self.p2[0] - self.p1[0]) + \
(point[1] - self.p1[1]) * (self.p2[1] - self.p1[1])) / segmentLength

def distanceToSquared(self, point):
t = self.getRatio(point)

if t < 0:
return distSquared(point, self.p1)
if t > 1:
return distSquared(point, self.p2)

return distSquared(point, [
self.p1[0] + t * (self.p2[0] - self.p1[0]),
self.p1[1] + t * (self.p2[1] - self.p1[1])
])

def distanceTo(self, point):
return math.sqrt(self.distanceToSquared(point))


def simplifyDouglasPeucker(points, pointsToKeep):
weights = []
length = len(points)

def douglasPeucker(start, end):
if (end > start + 1):
line = Line(points[start], points[end])
maxDist = -1
maxDistIndex = 0

for i in range(start + 1, end):
dist = line.distanceToSquared(points[i])
if dist > maxDist:
maxDist = dist
maxDistIndex = i

weights.insert(maxDistIndex, maxDist)

douglasPeucker(start, maxDistIndex)
douglasPeucker(maxDistIndex, end)

douglasPeucker(0, length - 1)
weights.insert(0, float("inf"))
weights.append(float("inf"))

weightsDescending = weights
weightsDescending = sorted(weightsDescending, reverse=True)

maxTolerance = weightsDescending[pointsToKeep - 1]
result = [
point for i, point in enumerate(points) if weights[i] >= maxTolerance
]

return result

关于 python : Ramer-Douglas-Peucker (RDP) algorithm with number of points instead of epsilon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37946754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com