The main idea : first give a circle radius and center, and then give a polygon all the points (polygon according to clockwise or counterclockwise), this polygon is convex, if it is convex polygon in judging whether the circle is within this convex polygon. Analysis: Judge Convex polygon can use adjacent three points cross product judgment, because do not know clockwise or counterclockwise, so cross product if there are integers and negative numbers, then must not be convex polygon (note allow more points on a line). Judge the circle in the convex polygon first to determine whether the center is within the polygons, if within the polygon, again to determine the center of the circle to reach the minimum distance of each edge, if less than the radius is not legal. PS: A good question, through this problem learned a lot of things. The code is as follows:=========================================================================================================== ============================
#include <stdio.h>#include<math.h>#include<algorithm>using namespacestd;Const intMAXN = 1e3+7;Const DoubleEPS = 1e-8;Const DoubleOO = 1e10+7;structpoint{Doublex, y; Point (Doublex=0,Doubley=0): X (x), Y (y) {} pointoperator- (ConstPoint &tmp)Const{ returnPoint (x-tmp.x, ytmp.y); } Double operator^(ConstPoint &tmp)Const{ returnx*tmp.y-y*tmp.x; } Double operator*(ConstPoint &tmp)Const{ returnx*tmp.x + y*tmp.y; }};DoubleDist (Point A, point B) {///the distance between two points returnsqrt (A-B) * (Ab));}intSign (Doublet) { if(T > EPS)return 1; if(Fabs (T) < EPS)return 0; return-1;///Negative number}structsegment{Point S, E; Segment (Point S=0, point e=0): s (s), E (e) {}BOOLONSEG (ConstPoint &p) {///whether the point is on a line segment if(sign (S-E) ^ (p-e)) = =0)///co-line if(sign (p.x-s.x) * (p.x-e.x)) <=0)///in the middle or end of a segment if(sign (P.Y-S.Y) * (P.Y-E.Y)) <=0) return true; return false; } BOOLInter (ConstSegment &tmp) {///consider only the case of complete intersection returnSign ((S-E) ^ (TMP. S-E)) * SIGN ((S-E) ^ (TMP. E-E)) = =-1; } Point Nearpoint (ConstPoint &p) {///Point to the nearest point of the segmentPoint Res; DoubleR = ((e-s) * (p-s))/((e-s) * (E-R)); if(R > EPS && (1.0-R) >EPS) {///Point is projected on a line segment on a line segmentRes.x = s.x + R * (e.x-s.x); Res.y= S.y + R * (e.y-s.y); } Else {///Find the nearest endpoint if(Dist (P, S) <Dist (P, E)) Res=S; ElseRes=E; } returnRes; }};structpoly{intN; Point VERTEX[MAXN]; BOOLIsconvex () {///Judging whether it is a convex polygon, can be collinear intvis[3] = {0}; for(intI=0; i<n; i++) {///if both integers and negative numbers are present, a concave intK = sign (vertex[(i+1)%n]-vertex[i] ^ (vertex[(i+2)%n]-vertex[i])); Vis[k+1] =1; if(vis[0] && vis[2]) return false; } return true; } intInpoly (ConstPoint &Q) {///determine if the point q is within the polygon, the X-ray method, the odd number, the even///returns 0 on a circle, outside of circle-1, Inside Circle 1Segment Ray (Point (-oo, q.y), Q);///farthest from the construction Ray intCnt=0;///Count the number of edges that intersect for(intI=0; i<n; i++) {Segment edge (Vertex[i], vertex[(i+1)%N]); if(Edge. Onseg (Q) = =true) return 0;///dot on the edge if(Ray. Onseg (vertex[i]) = =true) {///If you intersect a connection point, take a point with a small Y value if(Vertex[(i+1)%n].y-vertex[i].y >EPS) CNT++; } Else if(Ray. Onseg (vertex[(i+1)%N]) = =true) { if(Vertex[i].y-vertex[(i+1)%n].y >EPS) CNT++; } Else if(Ray. Inter (Edge) &&Edge. Inter (Ray)) CNT++; } if(CNT%2) return 1; Else return-1; }};structcircle{Point Center;///Center DoubleR///radius};BOOLFind (Poly &a, Circle &c) {///determine if a circle is inside a polygon if(A.inpoly (c.center) = =-1) return false;///if the center is outside the polygon for(intI=0; i<a.n; i++) {Segment edge (A.vertex[i], a.vertex[(i+1)%A.N]); DoubleLen =Dist (C.center, Edge. Nearpoint (C.center)); if(sign (LEN-C). R) <0) return false; } return true;}intMain () {Poly A;///Defining polygonsCircle C;///Defining a Circle while(SCANF ("%d", &A.N)! = EOF && A.N >2) {scanf ("%LF%LF%LF", &C.R, &c.center.x, &c.center.y); for(intI=0; i<a.n; i++) scanf ("%LF%LF", &a.vertex[i].x, &a.vertex[i].y); if(A.isconvex () = =false) printf ("HOLE is ill-formed\n"); Else if(Find (A, c) = =false) printf ("PEG would not fit\n"); Elseprintf ("PEG would fit\n"); } return 0;}
A Round Peg in A Ground hole-poj 1584 (judging convex polygons & judging points within polygons & judging circles within polygons)