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

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对两点间经行操作。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值