A Round Peg in a Ground Hole POJ - 1584(凸边形与圆相交)
作者:互联网
A Round Peg in a Ground Hole
POJ - 1584
题目链接:https://vjudge.net/problem/POJ-1584#author=0
题意:要求钉子要钉入孔内,判断能否在指定点钉入
思路:先判断这些点围成的多边形是不是凸多边形,如果是那么判断圆是否在凸边形里,若在那么就输出“PEG WILL FIT“,不在凸边形里就输出“PEG WILL NOT FIT”,如果都不是凸边形那么输出“HOLE IS ILL-FORMED”
// // Created by HJYL on 2020/1/22. // #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<cmath> using namespace std; const int maxn=250; const double eps=1e5; struct Point{ double x,y; Point(double x = 0, double y = 0):x(x),y(y){} Point operator - (const Point &B) { return Point(x-B.x, y-B.y); } }p[maxn]; int dcmp1(double x) { if (fabs(x)<0) return 0; else return x<0?-1:1; } bool operator == (const Point &a, const Point &b){ return dcmp1(a.x-b.x) == 0 && dcmp1(a.y-b.y) == 0; } double Cross(Point A,Point B) { return A.x*B.y-A.y*B.x; } double dot(Point A,Point B) { return A.x*B.x+A.y*B.y; } double Len(Point A) { return sqrt(A.x*A.x+A.y*A.y); } bool Istu(Point *p,int n) { p[n]=p[0]; p[n+1]=p[1]; int cha=dcmp1(Cross(p[1]-p[0],p[2]-p[1])); for(int i=1;i<n;i++) { int cha1=dcmp1(Cross(p[i+1]-p[i],p[i+2]-p[i+1])); if(cha*cha1<0) return false; cha=cha1; } return true; } int Isintu(Point *p,int n,Point pp) { p[n]=p[0]; int cha=dcmp1(Cross(p[0]-pp,p[1]-pp)); for(int i=1;i<n;i++) { int cha1=dcmp1(Cross(p[i]-pp,p[i+1]-pp)); if(cha1*cha<0) return -1; else if(cha1*cha==0) return 0; cha=cha1; } return 1; } double DistanceToSegment(Point P, Point A, Point B) { if(A == B) return Len(P-A); Point v1 = B-A; Point v2 = P-A; Point v3 = P-B; if(dcmp1(dot(v1, v2)) < 0) return Len(v2); else if(dcmp1(dot(v1, v3)) > 0) return Len(v3); else return fabs(Cross(v1, v2))/ Len(v1); } struct Circle{ Point middle; double radius; }c; bool Iscircleintu(Point *p,int n,Circle c) { int flag=Isintu(p,n,c.middle); if(flag==0) { if(c.radius == 0) return true; else return false; } else if(flag == 1) { p[n] = p[0]; for(int i = 0; i < n; i++) { if(dcmp1(DistanceToSegment(c.middle, p[i], p[i+1])-c.radius) < 0) { return false; } } return true; } else return false; } int main() { int n; while(~scanf("%d",&n)) { if(n<3) break; scanf("%lf%lf%lf",&c.radius,&c.middle.x,&c.middle.y); for(int i=0;i<n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); } bool result=Istu(p,n); if(result) { int result1=Iscircleintu(p,n,c); if(result1) printf("PEG WILL FIT\n"); else printf("PEG WILL NOT FIT\n"); } else printf("HOLE IS ILL-FORMED\n"); } return 0; }
标签:return,Point,int,double,形与,1584,凸边,include 来源: https://www.cnblogs.com/Vampire6/p/12229300.html