C++:道格拉斯-普客算法节点抽稀

C++:道格拉斯-普客算法节点抽稀
⼀、道格拉斯-普客算法(Douglas-Peucker algorithm, DP)
算法介绍推荐以下⽹址:
⼆、C++ 代码
分享给有需要的⼈,代码质量勿喷。
//system
#include <vector>
里约欢迎你#include <list>
/* Douglas-Peucker 抽稀 */
bool xjDouglasPeucker::xjDouglasPeuckerVacuate(std::vector<xjPoint> &xjVacuatedPoint, std::vector<xjPoint> &dataPoint, const double &thrDistance) {
try
{
if (dataPoint.size() < 3)
螺旋锥蝇
return false;
xjVacuatedPoint.clear();
int firstIndex = 0;
int endIndex = dataPoint.size() - 1;
//⾸尾点不能⼀样
double xf = dataPoint[firstIndex].x;
double yf = dataPoint[firstIndex].y;
double xl = dataPoint[endIndex].x;
double yl = dataPoint[endIndex].y;
/
/while (dataPoint[firstIndex] == (dataPoint[endIndex]))
while (((abs(xf - xl)) < 1e-15) && ((abs(yf - yl)) < 1e-15))
{
endIndex--;
}
常州新北区五个月宝宝致死案std::list<int> listKeyPointIndex;
listKeyPointIndex.push_back(firstIndex);
listKeyPointIndex.push_back(endIndex);
//递归
xjRecursionReduction(listKeyPointIndex, dataPoint, firstIndex, endIndex, thrDistance);
//排序
海安县紫石中学
listKeyPointIndex.sort();
std::list<int>::iterator iter;
for (iter = listKeyPointIndex.begin(); iter != d(); iter++)
{
xjVacuatedPoint.push_back(dataPoint[*iter]);
}
return true;
}
catch (const std::exception&)
{
return false;
}
}
/* 递归 */
void xjDouglasPeucker::xjRecursionReduction(std::list<int> &listKeyPointIndex,const std::vector<xjPoint> &dataPoint, int firstIndex, int endIndex, const double &thr {
int maxIndex = -3;
double maxDistance = -3;
for (int i = firstIndex; i < endIndex; i++)
{
double dis = xjDistancePL(dataPoint[i], dataPoint[firstIndex], dataPoint[endIndex]);
if (dis > maxDistance)
{
maxDistance = dis;
maxIndex = i;
}
}
if ((maxDistance > thrDistance) && (maxIndex != 0))
{
//添加最⼤距离点
listKeyPointIndex.push_back(maxIndex);
//左侧
xjRecursionReduction(listKeyPointIndex, dataPoint, firstIndex, maxIndex, thrDistance);
//右侧
xjRecursionReduction(listKeyPointIndex, dataPoint, maxIndex, endIndex, thrDistance);
}离心泵特性曲线
}
/* 点(p0) 到直线L(p1,p2) 的距离 */
double xjDouglasPeucker::xjDistancePL(const xjPoint &p0, const xjPoint &p1, const xjPoint &p2)
{
//area = |(1/2)(x1y2 + x2y3 + x3y1 - x2y1 - x3y2 - x1y3)|  //海伦公式计算三⾓形⾯积
//base = sqrt((x1-x2)2+(x1-x2)2)                            //三⾓形的底
//area = 0.5*base*H
//Height = area/0.5/base
double area = abs(0.5 * (p1.x * p2.y + p2.x * p0.y + p0.x * p1.y - p2.x * p1.y - p0.x * p2.y - p1.x * p0.y));
double base = sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
double height = area / base * 2;
return height;
}
三、结果
山东理工大学商学院

本文发布于:2024-09-20 20:25:57,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/649752.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:算法   新北区   需要
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议