小编典典

优化/简化路径

algorithm

假设我有一条包含150个节点/顶点的路径。如果这样,我如何简化呢?例如,一条具有3个顶点的直线会删除中间的顶点,因为它不添加任何路径。另外,如何避免破坏尖角?以及如何消除细微的变化并保留平滑的曲线。

谢谢


阅读 282

收藏
2020-07-28

共1个答案

小编典典

更简单的方法。取3个顶点a,b和c以及计算:顶点之间的角度/倾斜度。

std::vector<VERTEX> path;
//fill path
std::vector<int> removeList;
//Assure that the value is in the same unit as the return of angleOf function.
double maxTinyVariation = SOMETHING;

for (int i = 0; i < quantity-2; ++i) {
  double a = path[i], b = path[i + 1] , c = path[i + 2]
  double abAngle = angleOf(a, b);
  double bcAngle = angleOf(b, c);

  if (abs(ab - bc) <= maxTinyVariation) {
     //a, b and c are colinear
     //remove b later
     removeList.push_back(i+1);
  }
}
//Remove vertecies from path using removeList.
2020-07-28