c++ - 帮助理解这个算法

标签 c++ algorithm vector

我想实现 Ramer–Douglas–Peucker_algorithm在 C++ 中。

伪代码如下所示:

function DouglasPeucker(PointList[], epsilon)
 //Find the point with the maximum distance
 dmax = 0
 index = 0
 for i = 2 to (length(PointList) - 1)
  d = OrthogonalDistance(PointList[i], Line(PointList[1], PointList[end])) 
  if d > dmax
   index = i
   dmax = d
  end
 end

 //If max distance is greater than epsilon, recursively simplify
 if dmax >= epsilon
  //Recursive call
  recResults1[] = DouglasPeucker(PointList[1...index], epsilon)
  recResults2[] = DouglasPeucker(PointList[index...end], epsilon)

  // Build the result list
  ResultList[] = {recResults1[1...end-1] recResults2[1...end]}
 else
  ResultList[] = {PointList[1], PointList[end]}
 end

 //Return the result
 return ResultList[]
end

这是我目前的理解。 它是一个接受点数组和距离阈值的递归函数。

然后它遍历当前点以找到具有最大距离的点。

我对正交距离函数有些迷惑。我如何计算这个?我从未见过以线段为参数的距离函数。

我认为除此之外我应该没问题,我将只对数组使用 std::vectors。我想我会使用 std::copy,然后根据算法所说的进行推送或弹出。

谢谢

最佳答案

OrthogonalDistance 如下图所示:

alt Orthogonal

所以这是从你的点到直线上的点的距离,它是该点在直线上的投影。

点到线的距离通常是这样的:

alt text
(来源:fauser.edu)

其中x0y0是外点的坐标,ab c 是你的直线方程的系数。

那是很久以前我在学校的内存。

关于c++ - 帮助理解这个算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3495539/

相关文章:

c++ - 如何在 C++ 中过滤字符串 vector ?

C++ - std::set 未声明

python - 在 C/C++ 中嵌入 Python

algorithm - 我可以使用什么算法在图像上应用鱼眼失真?

c++ - 从c++中的函数返回数组 vector

Python:循环遍历两个向量的值

c++ - 逐字阅读文本文件时,如何忽略 "end of line"或 "new line"字符?

c++ - "="运算符返回什么?

algorithm - 极坐标 map 生成

mysql - SQL Select Query中的Where条件算法