POJ-1548 A Round Peg in a Ground Hole 凸多边形

  题目链接:http://poj.org/problem?id=1584

  首先判断是否为凸多边形,叉积判断即可,然后判断点是否在多边形内,先用叉积然后点到直线距离。

 1 //STATUS:C++_AC_0MS_192KB

 2 #include<stdio.h>

 3 #include<stdlib.h>

 4 #include<string.h>

 5 #include<math.h>

 6 #include<iostream>

 7 #include<string>

 8 #include<algorithm>

 9 #include<vector>

10 #include<queue>

11 #include<stack>

12 using namespace std;

13 #define LL __int64

14 #define pii pair<int,int>

15 #define Max(a,b) ((a)>(b)?(a):(b))

16 #define Min(a,b) ((a)<(b)?(a):(b))

17 #define mem(a,b) memset(a,b,sizeof(a))

18 #define lson l,mid,rt<<1

19 #define rson mid+1,r,rt<<1|1

20 const int N=210,M=1000000,INF=0x3f3f3f3f,MOD=1999997;

21 const LL LLNF=0x3f3f3f3f3f3f3f3fLL;

22 const double DNF=100000000;

23 

24 struct Node{

25     double x,y;

26 }nod[N],peg;

27 int n;

28 double pr;

29 

30 double disln(Node &l1,Node &l2,Node &o){

31     double A,B,C;

32     A=-(l1.y-l2.y);

33     B=l1.x-l2.x;

34     C=-A*l1.x-B*l1.y;

35     return fabs(A*o.x+B*o.y+C)/sqrt(A*A+B*B);

36 }

37 

38 inline void getr(Node &r,Node *a)

39 {

40     r.x=a[1].x-a[0].x;

41     r.y=a[1].y-a[0].y;

42 }

43 

44 int ispro(Node *a)

45 {

46     int i,ok;

47     double ini;

48     Node r1,r2;

49     getr(r1,a);getr(r2,a+1);

50     ini=r1.x*r2.y-r2.x*r1.y;

51     for(i=2;i<=n;i++){

52         r1=r2;

53         getr(r2,a+i);

54         if((r1.x*r2.y-r2.x*r1.y)*ini<0)return 0;

55     }

56 

57     return 1;

58 }

59 

60 int isconcir(Node *a)

61 {

62     int i,j;

63     Node r1,r2;

64     double ini;

65     for(i=0;i<n;i++)

66         if(disln(a[i],a[i+1],peg)<pr)return 0;

67     getr(r1,a);

68     r2.x=peg.x-a[0].x;r2.y=peg.y-a[0].y;

69     ini=r1.x*r2.y-r2.x*r1.y;

70     for(i=1;i<n;i++){

71         getr(r1,a+i);

72         r2.x=peg.x-a[i].x;r2.y=peg.y-a[i].y;

73         if((r1.x*r2.y-r2.x*r1.y)*ini<0)return 0;

74     }

75     return 1;

76 }

77 

78 int main()

79 {

80  //   freopen("in.txt","r",stdin);

81     int i,j;

82     while(~scanf("%d",&n) && n>2)

83     {

84         scanf("%lf%lf%lf",&pr,&peg.x,&peg.y);

85         for(i=0;i<n;i++){

86             scanf("%lf%lf",&nod[i].x,&nod[i].y);

87         }

88         nod[n].x=nod[0].x;nod[n].y=nod[0].y;

89         nod[n+1].x=nod[1].x;nod[n+1].y=nod[1].y;

90 

91         if(!ispro(nod))printf("HOLE IS ILL-FORMED\n");

92         else if(isconcir(nod))printf("PEG WILL FIT\n");

93         else printf("PEG WILL NOT FIT\n");

94     }

95     return 0;

96 }

 

你可能感兴趣的:(round)