poj1584A Round Peg in a Ground Hole
题意甚是难懂!这是第二遍做这道题了,依旧无法理解题意,搜了下题意。。。
首先需要判断是不是为凸多边形。(从一个顶点走一遍即可,要注意顺逆时针,题目中没有指明)
其次看一下圆是不是能够放入多边形内。(首先判断一下圆心是否在圆内,然后枚举圆心到所有边的距离与半径r进行比较)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 10000 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-8; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 #define _sign(x) ((x) > eps?1:((x) < -eps ? 2 :0)) 18 struct Point 19 { 20 double x,y; 21 Point(double x=0,double y=0):x(x),y(y) {} 22 }p[N],ch[N]; 23 int top; 24 struct Circle 25 { 26 Point c; 27 double r; 28 //Circle(Point c,double r):c(c),r(r){} 29 Point point (double a) 30 { 31 return Point(c.x+cos(a)*r,c.y+sin(a)*r); 32 } 33 }; 34 typedef Point pointt; 35 pointt operator + (Point a,Point b) 36 { 37 return Point(a.x+b.x,a.y+b.y); 38 } 39 pointt operator - (Point a,Point b) 40 { 41 return Point(a.x-b.x,a.y-b.y); 42 } 43 double cross(Point a,Point b) 44 { 45 return a.x*b.y-a.y*b.x; 46 } 47 double mul(Point p0,Point p1,Point p2) 48 { 49 return cross(p1-p0,p2-p0); 50 } 51 double dis(Point a) 52 { 53 return sqrt(a.x*a.x+a.y*a.y); 54 } 55 //精度判正负 56 int dcmp(double x) 57 { 58 if(fabs(x)<eps) return 0; 59 else return x<0?-1:1; 60 } 61 int Graham(int n) 62 { 63 int i,s[3] = {1,1,1}; 64 double t; 65 for(i = 0; i < n&&s[1]|s[2]; i ++) 66 { 67 t = mul(p[(i+1)%n],p[(i+2)%n],p[i]); 68 s[_sign(t)] = 0; 69 } 70 return s[1]|s[2]; 71 } 72 double distoline(Point p,Point a,Point b) 73 { 74 Point v1 = b-a,v2 = p-a; 75 return fabs(cross(v1,v2)/dis(v1)); 76 } 77 int inside(Point po,int n) 78 { 79 int i,s[3] = {1,1,1}; 80 double t; 81 for(i = 0;i < n&&s[1]|s[2];i ++) 82 { 83 t = mul(p[(i+1)%n],po,p[i]); 84 s[_sign(t)] = 0; 85 } 86 return s[1]|s[2]; 87 } 88 int main() 89 { 90 int n,i; 91 Circle cc; 92 while(scanf("%d",&n)!=EOF) 93 { 94 if(n<3) break; 95 scanf("%lf%lf%lf",&cc.r,&cc.c.x,&cc.c.y); 96 for(i = 0; i < n; i++) 97 scanf("%lf%lf",&p[i].x,&p[i].y); 98 top = 0; 99 int m = Graham(n); 100 if(!m) 101 { 102 puts("HOLE IS ILL-FORMED"); 103 continue; 104 } 105 int flag = 1; 106 if(!inside(cc.c,n)) 107 { 108 puts("PEG WILL NOT FIT"); 109 continue; 110 } 111 for(i = 1; i < n; i ++) 112 { 113 if(dcmp(distoline(cc.c,p[i],p[i-1])-cc.r)<0) 114 { 115 flag = 0; 116 break; 117 } 118 } 119 if(!flag) 120 puts("PEG WILL NOT FIT"); 121 else 122 puts("PEG WILL FIT"); 123 } 124 return 0; 125 }