POJ 1474 Video Surveillance(半平面交)

题目链接

2Y,模版抄错了一点。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 #define eps 1e-8
 8 #define N 2001
 9 struct point
10 {
11     double x,y;
12 }p[N],pre[N],temp[N];
13 double a,b,c;
14 int n,m;
15 void getline(point x,point y)
16 {
17     a = y.y - x.y;
18     b = x.x - y.x;
19     c = y.x * x.y - x.x * y.y;
20 }
21 point intersect(point x,point y)
22 {
23     double u = fabs(a*x.x + b*x.y + c);
24     double v = fabs(a*y.x + b*y.y + c);
25     point ans;
26     ans.x = (x.x*v+y.x*u)/(u+v);
27     ans.y = (x.y*v+y.y*u)/(u+v);
28     return ans;
29 }
30 void cut()
31 {
32     int num = 0,i;
33     for(i = 1;i <= m;i ++)
34     {
35         if(a*p[i].x + b*p[i].y + c > -eps)
36         {
37             temp[++num] = p[i];
38         }
39         else
40         {
41             if(a*p[i-1].x + b*p[i-1].y + c > eps)
42             temp[++num] = intersect(p[i],p[i-1]);
43             if(a*p[i+1].x + b*p[i+1].y + c > eps)
44             temp[++num] = intersect(p[i],p[i+1]);
45         }
46     }
47     for(i = 1;i <= num;i ++)
48     p[i] = temp[i];
49     p[0] = p[num];
50     p[num+1] = p[1];
51     m = num;
52 }
53 void fun()
54 {
55     int i;
56     m = n;
57     for(i = 1;i <= n;i ++)
58     {
59         getline(pre[i],pre[i+1]);
60         cut();
61     }
62 }
63 int main()
64 {
65     int i,cas = 1;
66     while(scanf("%d",&n)!=EOF)
67     {
68         if(!n) break;
69         for(i = 1;i <= n;i ++)
70         {
71             scanf("%lf%lf",&pre[i].x,&pre[i].y);
72             p[i] = pre[i];
73         }
74         pre[n+1] = pre[1];
75         p[n+1] = p[1];
76         p[0] = p[n];
77         fun();
78         printf("Floor #%d\n",cas ++);
79         if(m)
80         printf("Surveillance is possible.\n");
81         else
82         printf("Surveillance is impossible.\n");
83         printf("\n");
84     }
85     return 0;
86 }

 

posted @ 2013-10-15 16:54  Naix_x  阅读(177)  评论(0编辑  收藏  举报