比较水的题,但是要细心,把情况考虑清楚
#include <cstdio> #include <algorithm> #include <cmath> using namespace std; const int maxn=10000; const double eps=1e-8; int n,f[maxn]; double rad; struct Point { double x,y; }; Point ver[maxn],pel; double cross(Point a,Point b,Point c,Point d) { return (b.x-a.x)*(d.y-c.y)-(b.y-a.y)*(d.x-c.x); } double dis(Point a,Point b) { return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y)); } double getD(Point a,Point b,Point c) { return fabs(cross(a,b,a,c))/dis(a,b); } int main() { while(scanf("%d",&n)&&n>=3) { scanf("%lf%lf%lf",&rad,&pel.x,&pel.y); int i; for(i=0;i<n;i++) scanf("%lf%lf",&ver[i].x,&ver[i].y); ver[n].x=ver[0].x;ver[n].y=ver[0].y; int flag=1; for(i=2;i<=n;i++) { double tem=cross(ver[i-2],ver[i-1],ver[i-1],ver[i]); if(fabs(tem)<eps) f[i]=0; else if(tem<0) f[i]=-1; else f[i]=1; } int ju=0; for(i=3;i<=n;i++) { if(f[i]*f[i-1]<0) { flag=0; break; } else { if(f[i]!=0) ju=f[i]; } } if(!flag) printf("HOLE IS ILL-FORMED\n"); else { flag=1; int flag2=1; if(ju==1) { for(i=0;i<n;i++) { if(cross(ver[i],ver[i+1],ver[i],pel)<eps) { flag=0; break; } } } else if(ju==-1) { for(i=0;i<n;i++) { if(cross(ver[i],ver[i+1],ver[i],pel)>-eps) { flag=0; break; } } } else flag=0; if(flag) { for(i=0;i<n;i++) { if(getD(ver[i],ver[i+1],pel)-rad<=-eps) { flag2=0; break; } } } if(flag&&flag2) printf("PEG WILL FIT\n"); else printf("PEG WILL NOT FIT\n"); } } return 0; }