标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5441 | Accepted: 1729 |
Description
Input
Output
Sample Input
5 1.5 1.5 2.0 1.0 1.0 2.0 2.0 1.75 2.0 1.0 3.0 0.0 2.0 5 1.5 1.5 2.0 1.0 1.0 2.0 2.0 1.75 2.5 1.0 3.0 0.0 2.0 1
Sample Output
HOLE IS ILL-FORMED PEG WILL NOT FIT
题意:判断一个多边形是否凸包且内含给定圆,包含给定圆可内切(这里WA了一次)
思路:先通过叉积判断凸包,再判断圆心是否在多边形内,再判断是否内含或内切
/** * Start at 12:33 * End at 13:17 * Problem: poj1584 * Author: __560 * */ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<math.h> using namespace std; const int maxn=1000100; const int INF=(1<<28); const double eps=0.00000001; int n; struct Point { double x,y; friend double operator*(const Point A,const Point B) { return A.x*B.y-A.y*B.x; } friend Point operator-(const Point A,const Point B) { return {A.x-B.x,A.y-B.y}; } }; Point vert[maxn],peg; double R; bool convex_bag(Point*vert,int n) ///判断是否凸包 { for(int i=0;i<n;i++){ if(((vert[(i+2)%n]-vert[(i+1)%n])*(vert[(i+1)%n]-vert[i]))*((vert[(i+3)%n]-vert[(i+2)%n])*(vert[(i+2)%n]-vert[(i+1)%n]))<-eps) return false; } return true; } bool isin(Point*vert,int n,Point peg) ///判断点是否在多边形内 { for(int i=0;i<n;i++){ if(((vert[i]-peg)*(vert[(i+1)%n]-peg))*((vert[(i+1)%n]-peg)*(vert[(i+2)%n]-peg))<eps) return false; } return true; } double dist(Point A,Point B) { return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2)); } bool contain_circle(Point*vert,int n,Point peg,double R) ///判断多边形是否内含或内切已知圆 { for(int i=0;i<n;i++){ double Area=fabs((vert[(i+1)%n]-peg)*(vert[i]-peg)); double d=Area/dist(vert[i],vert[(i+1)%n]); if(d<R-eps) return false; } return true; } int main() { //freopen("out.txt","w",stdout); while(cin>>n,n>=3){ cin>>R>>peg.x>>peg.y; for(int i=0;i<n;i++) scanf("%lf%lf",&(vert[i].x),&(vert[i].y)); if(!convex_bag(vert,n)){ printf("HOLE IS ILL-FORMED\n"); continue; } if(!isin(vert,n,peg)){ printf("PEG WILL NOT FIT\n"); continue; } if(contain_circle(vert,n,peg,R)) printf("PEG WILL FIT\n"); else printf("PEG WILL NOT FIT\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/--560/p/4389817.html