您的位置:首页 > 其它

暴力法解凸包

2016-02-02 17:36 225 查看
给定平面上一系列的点,用暴力法求解它们的凸包,此算法比普通的暴力法要优化,用新找到的极点去寻找下一个极点。此算法不能用于任何两个点在一直线上的情况。

输入 ConvexHull.txt

7,8
10,17
14,14
15,23
16,12
17,3
22,17
24,4
26,18

C代码

/*brute force solution to convex hull problem  */
/*only limited to there is no point on the same line with any other point*/
/*use the new extreme point to find the next*/
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100
#define MAXDISTANCE 100000
struct Point
{
int x;
int y;
} points[MAX];

int main()
{
FILE *fp;
if((fp=fopen("ConvexHull.txt","r"))==NULL)
{
printf("Cannot open file !");
exit(1);
}
int n=0;
int a=0;
int b=0;
int c=0;
int sign=0;
int cmp;//compare with sign
int point;

int count=0;
int start=-1;
int pre=-1;

while(!feof(fp))
{
fscanf(fp,"%d,%d",&points
.x,&points
.y);
n++;
}

int convexHull
= {-1};

for(int i=0; i<n; i++)
printf("No %d point:(%d,%d)\n",i+1,points[i].x,points[i].y);

int one=0;
int two=0;

while(one<n&&one!=start)
{
for(two=0; two<n; two++)//only calculate two=one;two<n is wrong, one's two can be smaller than one
{
if(one!=two&&two!=pre)
{
a=points[two].y-points[one].y;
b=points[one].x-points[two].x;
c=points[one].x*points[two].y-points[one].y*points[two].x;
sign=0;   //whether ax+by-c is postive or not
for(point=0; point<n; point++)
if(point!=one&&point!=two)
{
cmp=a*points[point].x+b*points[point].y-c; //cmp can be zero, if cmp is zero it's on the line,one and two are can be still extreme points

if(sign==0)
sign=cmp;//if the first cmp is zero ,it can not be used for sign,thus sign will be assgined again
else if(sign*cmp<0)
break;

}
if(point==n)//all points on the same side
{
//    printf("%d and %d are extreme points!\n",one,two);
if(start==-1)
start=one;
printf("(%d,%d) is extreme point!\n",points[one].x,points[one].y);
pre=one;
one=two;//not all one will ++,some one depend on two but if there is no extreme point two then one will ++
/*use the new extreme point to find the next*/
break;
}
}
}//for(two=0; two<n; two++)
if(two==n)
one++;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: