您的位置:首页 > 编程语言 > C语言/C++

C++道格拉斯-普克 Douglas-Peuker(DP算法)

2015-11-22 19:10 573 查看
double Distance(int x1,int y1,int x2,int y2)
{
double distance;
distance=(double)sqrt(double(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
return distance;
}
double NDistance(int x1,int y1,int x2,int y2,int x3,int y3)
{
double A=y1-y2;
double B=x2-x1;
int C=-B*y1-A*x1;
double ndistance=fabs(float(A*x3+B*y3+C))/sqrt(pow(A,2)+ pow(B,2));
return ndistance;
}

/////////////////////////////////////////////
//////Douglas一Peukcer算法
void	CMyDoc::getNDistance(vector<pair<int, int>>::iterator pVector,vector<pair<int, int>>::iterator pVector1,int limit)
{

vector<pair<int, int>>::iterator testVector;
vector<pair<int, int>>::iterator testVector1;
vector<pair<int, int>>::iterator tempVector;
tempVector=pVector;
if(pVector1==(tempVector++))
return;
tempVector=pVector;
int MaxNum=0;
double MaxDistance=0.0;
for(tempVector;tempVector!=pVector1;)
{
testVector=(tempVector++);
double temp;
temp=NDistance((*pVector).first,(*pVector).second,(*pVector1).first,(*pVector1).second,(*testVector).first,(*testVector).second);
if(MaxDistance<temp)
{
MaxDistance=temp;
testVector1=testVector;
}
}
tempVector=pVector;
if(MaxDistance<limit)
{
for( tempVector++;tempVector!= pVector1;tempVector++)
{
(*tempVector).first=8080;
(*tempVector).second=8080;
//point.erase(pVector);
}
return;
}
getNDistance(pVector,testVector1, limit);     //迭代
getNDistance(testVector1,pVector1, limit);

}


2维的Douglas-Peuker算法的算法函数,通过向里面传递vector 的iterator对两点间经行操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ dp