• 暴力法解凸包


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

    输入 ConvexHull.txt

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

    C代码

     1 /*brute force solution to convex hull problem  */
     2 /*only limited to there is no point on the same line with any other point*/
     3 /*use the new extreme point to find the next*/
     4 #include<stdio.h>
     5 #include <stdlib.h>
     6 #include <math.h>
     7 #define MAX 100
     8 #define MAXDISTANCE 100000
     9 struct Point
    10 {
    11     int x;
    12     int y;
    13 } points[MAX];
    14 
    15 int main()
    16 {
    17     FILE *fp;
    18     if((fp=fopen("ConvexHull.txt","r"))==NULL)
    19     {
    20         printf("Cannot open file !");
    21         exit(1);
    22     }
    23     int n=0;
    24     int a=0;
    25     int b=0;
    26     int c=0;
    27     int sign=0;
    28     int cmp;//compare with sign
    29     int point;
    30     
    31     int count=0;
    32     int start=-1;
    33     int pre=-1;
    34 
    35     while(!feof(fp))
    36     {
    37         fscanf(fp,"%d,%d",&points[n].x,&points[n].y);
    38         n++;
    39     }
    40 
    41     int convexHull[n]= {-1};
    42 
    43 
    44     for(int i=0; i<n; i++)
    45         printf("No %d point:(%d,%d)
    ",i+1,points[i].x,points[i].y);
    46 
    47     int one=0;
    48     int two=0;
    49 
    50 
    51     while(one<n&&one!=start)
    52     {    
    53         for(two=0; two<n; two++)//only calculate two=one;two<n is wrong, one's two can be smaller than one 
    54         {
    55             if(one!=two&&two!=pre)
    56             {
    57                     a=points[two].y-points[one].y;
    58                     b=points[one].x-points[two].x;
    59                     c=points[one].x*points[two].y-points[one].y*points[two].x;
    60                     sign=0;   //whether ax+by-c is postive or not
    61                     for(point=0; point<n; point++)
    62                         if(point!=one&&point!=two)
    63                         {
    64                             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
    65                         
    66                             if(sign==0)
    67                                 sign=cmp;//if the first cmp is zero ,it can not be used for sign,thus sign will be assgined again
    68                             else if(sign*cmp<0)
    69                                 break;
    70 
    71                         }
    72                     if(point==n)//all points on the same side
    73                     {
    74                         //    printf("%d and %d are extreme points!
    ",one,two);
    75                         if(start==-1)
    76                             start=one;
    77                         printf("(%d,%d) is extreme point!
    ",points[one].x,points[one].y);
    78                         pre=one;
    79                         one=two;//not all one will ++,some one depend on two but if there is no extreme point two then one will ++ 
    80                         /*use the new extreme point to find the next*/
    81                         break;
    82                     }                
    83             }
    84         }//for(two=0; two<n; two++)
    85         if(two==n)
    86             one++;
    87     }
    88 
    89 
    90     return 0;
    91 }
  • 相关阅读:
    Linux学习笔记(八)权限管理命令
    Linux学习笔记(七)关机、重启及常用的网络命令
    Linux学习笔记(六)压缩和解压缩命令
    Linux学习笔记(五)搜索命令
    Linux学习笔记(四)帮助命令
    Linux学习笔记(三)目录和文件都能操作的命令
    Linux学习笔记(二)文件操作命令
    Linux学习笔记(一)目录处理命令
    面试题((A)null).fun()——java中null值的强转
    idea注册码
  • 原文地址:https://www.cnblogs.com/unflynaomi/p/5178155.html
Copyright © 2020-2023  润新知