POJ 1474 半平面交

题意:

判断多边形是否存在核。

 

题解:
最终的交点数组中没有点则没有核。

 

 

View Code
  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstdlib>
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <cmath>
  7 
  8 #define N 2222
  9 #define EPS 1e-7
 10 #define INF 1e9
 11 
 12 using namespace std;
 13 
 14 struct PO
 15 {
 16     double x,y;
 17 }p[N],tp[N],s[N],o;
 18 
 19 int n,cas;
 20 
 21 inline void read()
 22 {
 23     for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
 24     p[n+1]=p[1];
 25 }
 26 
 27 inline PO operator -(PO a,PO b)
 28 {
 29     PO c;
 30     c.x=a.x-b.x;
 31     c.y=a.y-b.y;
 32     return c;
 33 }
 34 
 35 inline int dc(double x)
 36 {
 37     if(x>EPS) return 1;
 38     else if(x<-EPS) return -1;
 39     return 0;
 40 }
 41 
 42 inline double cross(PO &a,PO &b,PO &c)
 43 {
 44     return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
 45 }
 46 
 47 inline double getarea(PO *a,int n)//多边形面积,逆时针为正 
 48 {
 49     double res=0.0;
 50     for(int i=1;i<=n;i++) res+=cross(o,a[i],a[i+1]);
 51     return res*0.5;
 52 }
 53 
 54 inline PO getpoint(PO &a,PO &b,PO &c,PO &d)//直线交点 
 55 {
 56     PO ans,tp=b-a;
 57     double k1=cross(a,d,c);
 58     double k2=cross(b,c,d);
 59     ans.x=a.x+tp.x*k1/(k1+k2);
 60     ans.y=a.y+tp.y*k1/(k1+k2);
 61     return ans;
 62 }
 63 
 64 inline void change()//变顺时针为逆时针 
 65 {
 66     for(int i=1;i<=(n>>1);i++) swap(p[i],p[n-i+1]);
 67     p[n+1]=p[1];
 68 }
 69 
 70 void prt(PO a)
 71 {
 72     printf("%lf   %lf\n",a.x,a.y);
 73 }
 74 
 75 inline void getcut()
 76 {
 77     tp[1].x=tp[5].x=-INF,tp[1].y=tp[5].y=-INF;
 78     tp[2].x=INF,tp[2].y=-INF;
 79     tp[3].x=INF,tp[3].y=INF;
 80     tp[4].x=-INF,tp[4].y=INF;
 81     int cp=4,tc;
 82     for(int i=1;i<=n;i++)
 83     {
 84         tc=0;
 85         for(int j=1;j<=cp;j++)
 86         {
 87             if(dc(cross(p[i],p[i+1],tp[j]))>=0) s[++tc]=tp[j];
 88             if(dc(cross(p[i],p[i+1],tp[j])*cross(p[i],p[i+1],tp[j+1]))<0)
 89                 s[++tc]=getpoint(p[i],p[i+1],tp[j],tp[j+1]);
 90         }
 91         s[tc+1]=s[1];
 92         for(int j=1;j<=tc+1;j++) tp[j]=s[j];
 93         cp=tc;
 94     }
 95     n=cp;
 96 }
 97 
 98 inline void go()
 99 {
100     change();
101     getcut();
102     if(n) printf("Floor #%d\nSurveillance is possible.\n\n",++cas);
103     else printf("Floor #%d\nSurveillance is impossible.\n\n",++cas);
104 }
105 
106 int main()
107 {
108     while(scanf("%d",&n),n) read(),go();
109     return 0;
110 }

 

 

posted @ 2013-02-26 23:39  proverbs  阅读(209)  评论(0编辑  收藏  举报