<转> 最小凸包算法(Convex Hull)(1)-Graham扫描法 -计算几何-算法导论

原文地址:http://blog.csdn.net/suwei19870312/article/details/542281

基本问题:

平面上有n个点p1,p2, ..., pn, 要求求出一个面积最小的凸多边形,使得这个多边形包含所有平面上的点。

 

根据算法导论上提供的两个方法做一些介绍:

算法1:

Graham扫描法

下面直接给出一段伪代码,方便描述:

GRAHAM-SCAN(Q)
{
     1. 取出所有点钟y坐标最小的点作为初始点p0
     2. 之后对于所有其他点,以p0为中心,点集中的所有点按关于p0的极角逆时针排序,形成p1,p2,..pn-1
     3. push(p0,S) 
     4. push(p1,S)
     5. push(P2.S)
     for(i: 3->m)
     {     
           px = nexttoTop(S)
           py = Top(S) 
           do while (如果(py->pi向量)相对于(px->py向量)是向右走的)
                     pop(S)
                     px = nextotTop(S)
                     py = Top(S)
           push(pi, S);
     }
     return S;
}

最后S栈中保存了所有凸多边形的顶点集合

 

下面用图示表示一下算法的过程:

1.初始化所有的p0,p1,...pn-1

 

2.  p0,p1,p2入栈

  

3. 这时候栈顶元素是p2,次栈顶元素p1, 枚举p3, 那么可以看出, p2->p3的向量相对于p2->p1的向量是向右走的,所以栈中弹出p2, 压入p3

 

 4. P4入栈,由于栈顶元素是p3,次栈顶元素是p1, 那么p3->p4向量,相对于p1->p3向量是向左走的,所以压入p4

 

 

5.由于栈顶元素是p4,次栈顶元素是p3, 那么p4->p5向量,相对于p3->p4向量是向右走的,所以弹出p4,压入p5

//xiaoxia版
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct
{
	double x;
	double y;
}POINT;
POINT result[102];							//保存凸包上的点,相当于所说的栈S 
POINT a[102];								
int n,top;
double Distance(POINT p1,POINT p2)			//两点间的距离
{
	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double Multiply(POINT p1,POINT p2,POINT p3) //叉积
{	
   return ((p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x)); 
}
int Compare(const void *p1,const void *p2) //根据p0->p1的极值和p0->p2的极值进行比较,如果极值相同则用距离长度比较 
{
	POINT *p3,*p4;
	double m;
    p3=(POINT *)p1; 
    p4=(POINT *)p2; 
	m=Multiply(a[0],*p3,*p4) ;
	if(m<0) return 1;
	else if(m==0&&(Distance(a[0],*p3)<Distance(a[0],*p4)))
		return 1;
	else return -1;
}
//寻找凸包的过程,p0,p1,p2..的寻找过程在下面main中进行了 
void Tubao()
{
   int i;
   result[0].x=a[0].x;
   result[0].y=a[0].y;
   result[1].x=a[1].x;
   result[1].y=a[1].y;
   result[2].x=a[2].x;
   result[2].y=a[2].y;
   top=2;
   for(i=3;i<=n;i++)
   {
       while(Multiply(result[top-1],result[top],a[i])<=0 && top>2)
			top--;
       result[top+1].x=a[i].x;
       result[top+1].y=a[i].y;
       top++;
   }
}
int main()
{
   int i,p;
   double px,py,len,temp;
   while(scanf("%d",&n)!=EOF,n)
   {
       for(i=0;i<n;i++)
           scanf("%lf%lf",&a[i].x,&a[i].y);
       if(n==1)
       {
           printf("0.00/n");
           continue;
       }
       else if(n==2)
       {
           printf("%.2lf/n",Distance(a[0],a[1]));
           continue;
       }
       
       //这里的目的好像是找出y坐标最小的点,之后把他定义为P0 
       py=-1;
       for(i=0;i<n;i++)
	   {
           if(py==-1 || a[i].y<py)
           {
               px=a[i].x;
               py=a[i].y;
			   p=i;
           }
           else if(a[i].y==py && a[i].x<px)
           {
               px=a[i].x;
               py=a[i].y;
			   p=i;
           }
	   }
	   //swap(a[0],a[p])
	   temp=a[0].x;
	   a[0].x=a[p].x;
	   a[p].x=temp;
	   temp=a[0].y;
	   a[0].y=a[p].y;
	   a[p].y=temp;
       
       
       //用叉乘来实现排序的比较 
       qsort(&a[1],n-1,sizeof(double)*2,Compare);
       a[n].x=a[0].x;
       a[n].y=a[0].y;
       
       //调用tubao() 
       Tubao();
       
       len=0.0;
       for(i=0;i<top;i++)
           len=len+Distance(result[i],result[i+1]);
       printf("%.2lf/n",len);
   }
   return 0;
}

  算法学习不断!

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Graham-Scan算法的C++实现。 ```cpp #include <bits/stdc++.h> using namespace std; struct Point { int x, y; }; // 叉积 int cross(const Point &a, const Point &b, const Point &c) { return (b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x); } // 求凸包,返回点集 vector<Point> convexHull(vector<Point> &points) { int n = points.size(); // 先按照x坐标从小到大排序,x坐标相同按y坐标从小到大排序 sort(points.begin(), points.end(), [](const Point &a, const Point &b) { return a.x == b.x ? a.y < b.y : a.x < b.x; }); // 求下凸壳 vector<Point> lower; for (int i = 0; i < n; ++i) { while (lower.size() >= 2 && cross(lower[lower.size() - 2], lower.back(), points[i]) <= 0) lower.pop_back(); lower.push_back(points[i]); } // 求上凸壳 vector<Point> upper; for (int i = n - 1; i >= 0; --i) { while (upper.size() >= 2 && cross(upper[upper.size() - 2], upper.back(), points[i]) <= 0) upper.pop_back(); upper.push_back(points[i]); } // 合并下凸壳和上凸壳 vector<Point> ans(lower); ans.insert(ans.end(), upper.begin() + 1, upper.end() - 1); return ans; } int main() { int n; cin >> n; vector<Point> points(n); for (int i = 0; i < n; ++i) cin >> points[i].x >> points[i].y; vector<Point> hull = convexHull(points); cout << "Convex Hull:" << endl; for (const Point &p : hull) cout << p.x << " " << p.y << endl; return 0; } ``` 在上述代码中: - `Point` 结构体表示一个点,包含 `x` 和 `y` 坐标; - `cross` 函数用于计算向量 $\overrightarrow{AB}$ 和 $\overrightarrow{AC}$ 的叉积,即 $(\overrightarrow{AB} \times \overrightarrow{AC})$,结果为正表示 $\overrightarrow{AB}$ 在 $\overrightarrow{AC}$ 的逆时针方向,结果为负表示 $\overrightarrow{AB}$ 在 $\overrightarrow{AC}$ 的顺时针方向,结果为 $0$ 表示 $\overrightarrow{AB}$ 与 $\overrightarrow{AC}$ 共线; - `convexHull` 函数用于求解凸包,输入为点集 `points`,输出为凸包点集; - 在函数内部,先按照 x 坐标从小到大排序,x 坐标相同按 y 坐标从小到大排序; - 接着求下凸壳,利用单调栈维护; - 再求上凸壳,同样利用单调栈维护; - 最后将下凸壳和上凸壳合并,得到最终的凸包点集。 希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值