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


  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Douglas-Peucker算法可以用C语言实现,以下是其中一个可能的实现: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct { double x; double y; } Point; // 计算点p1和点p2之间的欧几里得距离 double distance(Point p1, Point p2) { double dx = p1.x - p2.x; double dy = p1.y - p2.y; return sqrt(dx*dx + dy*dy); } // 计算点p到线段(p1,p2)的距离 double perpendicularDistance(Point p, Point p1, Point p2) { double area = fabs((p2.x-p1.x)*(p1.y-p.y)-(p1.x-p.x)*(p2.y-p1.y)); double lineLength = distance(p1, p2); return area / lineLength; } // 递归调用Douglas-Peucker算法 void douglasPeucker(Point* points, int start, int end, double epsilon, int* keep) { // 如果只有两个点,直接保留 if (end - start == 1) { keep[start] = 1; keep[end] = 1; return; } // 找出距离直线最远的点 double maxDistance = 0; int maxIndex = 0; Point p1 = points[start]; Point p2 = points[end]; for (int i = start+1; i < end; i++) { double d = perpendicularDistance(points[i], p1, p2); if (d > maxDistance) { maxDistance = d; maxIndex = i; } } // 如果最远距离大于epsilon,则继续递归,否则保留 if (maxDistance > epsilon) { douglasPeucker(points, start, maxIndex, epsilon, keep); douglasPeucker(points, maxIndex, end, epsilon, keep); } else { for (int i = start; i <= end; i++) { keep[i] = 1; } } } // Douglas-Peucker算法的入口函数 void simplify(Point* points, int n, double epsilon, Point** result, int* m) { // 先分配一个足够大的数组保留结果 int* keep = (int*)malloc(n * sizeof(int)); // 递归调用Douglas-Peucker算法 douglasPeucker(points, 0, n-1, epsilon, keep); // 统计结果 *m = 0; for (int i = 0; i < n; i++) { if (keep[i]) { (*m)++; } } *result = (Point*)malloc((*m) * sizeof(Point)); int j = 0; for (int i = 0; i < n; i++) { if (keep[i]) { (*result)[j++] = points[i]; } } free(keep); } int main() { Point points[] = { {0, 0}, {1, 1}, {2, 0}, {3, 1}, {4, 0}, {5, 1}, {6, 0}, {7, 1}, {8, 0}, {9, 1} }; int n = sizeof(points) / sizeof(Point); Point* simplifiedPoints; int m; simplify(points, n, 0.5, &simplifiedPoints, &m); printf("Original points:\n"); for (int i = 0; i < n; i++) { printf("%.1f, %.1f\n", points[i].x, points[i].y); } printf("Simplified points:\n"); for (int i = 0; i < m; i++) { printf("%.1f, %.1f\n", simplifiedPoints[i].x, simplifiedPoints[i].y); } free(simplifiedPoints); return 0; } ``` 该实现中,首先定义了一个结构体 `Point` 表示二维空间中的一个点,然后实现了 `distance` 函数用于计算点与点之间的距离,以及 `perpendicularDistance` 函数用于计算点到线段的垂直距离。 接下来是Douglas-Peucker算法的核心部分,实现了一个 `douglasPeucker` 函数用于递归调用算法,并实现了一个 `simplify` 函数作为入口函数。输入参数为原始点数组 `points`,点数量 `n`,阈值 `epsilon`,输出参数为简化后的点数组 `result` 和点数量 `m`。算法先分配一个 `keep` 数组用于标记需要保留的点,然后调用 `douglasPeucker` 函数进行递归计算。最后统计保留的点数量并返回简化后的点数组即可。 在 `main` 函数中,我们定义了一个包含10个点的测试数组,并调用 `simplify` 函数进行简化,然后打印出原始点和简化后的点供查看。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值