暴力法解凸包
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代码
输入 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; }
相关文章推荐
- python 多线程笔记(2)-- 锁
- 6. Java 加解密技术系列之 3DES
- tomcat内存配置优化
- Web服务器的工作原理
- 通过char **指针改变char*指针的指向
- PLSQL Developer备份恢复oracle数据
- 简单排序算法
- wsimport命令的使用
- 嵌入式Linux之设备驱动程序
- 当人手一部智能手机时 庞大的数据中心们已死
- 我与小娜(03):量子超距通信,保持联系未来
- BZOJ 3262 陌上花开、HDU 5618 Jam's problem again(三维偏序、cdq分治 + BIT)
- 根据文本内容快速自适应控件的宽度和高度
- WAPG4 解决透明导航栏返回黑块
- Android中短信发送倒计时方法
- Eclipse闪退/打不开/无法启动/一闪而过
- Android的生成日志文件
- 看懂UML类图
- Convert UIImage to Stream
- Github如何上传gif图片