Blog original address: http://blog.csdn.net/xuechelingxiao/article/details/39178777
A round peg in a ground hole
The vertex coordinates, circle radius, and center coordinates of the polygon are presented clockwise or counterclockwise.
1. Check whether the polygon is a convex hull. If it is not the output "hole is ill-formed ".
2. If the polygon is a convex hull, determine whether the circle is in the convex hull. If the convex hull is in the garden, output "peg will fit". If not, output "peg will not fit ".
Solution: the general idea of this question is to use the cross product to determine whether a polygon is convex. Because we did not tell the order of the given points, we need to determine whether the points are clockwise or counterclockwise, there is also the need to consider the side of the convex bag.
When judging whether the circle is in the convex bag, use the cross product to check whether the center and the cross product of the convex bag are always the same, then, determine whether the distance from the center to each side of the convex hull is less than or equal to the radius of the circle R (note that the distance is less than or equal ). For more information, see the code.
Put a small welfare: wa students can look at the http://midatl.fireduck.com/archive/2003/problems/MidAtlantic-2003/problem-D/
The Code is as follows:
#include <stdio.h>#include <algorithm>#include <math.h>const double eps = 1e-8;using namespace std;struct Point { double x, y;} P[2000], O;struct Line { Point a, b;} ;int dcmp(double x) { return x < -eps ? -1 : x > eps;}double xmult(Point a, Point b, Point c) { return (a.x-c.x)*(b.y-c.y) - (a.y-c.y)*(b.x-c.x);}double Distance(Point a, Point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double disptoseg(Point p, Line l){Point t = p;t.x += l.a.y-l.b.y, t.y += l.b.x-l.a.x;if(xmult(l.a,t,p)*xmult(l.b,t,p)>eps)return Distance(p,l.a) < Distance(p,l.b) ? Distance(p,l.a) : Distance(p,l.b);return fabs(xmult(p, l.a, l.b))/Distance(l.a, l.b);}int main(){ int n; double r; while(~scanf("%d", &n) && n >= 3) { scanf("%lf%lf%lf", &r, &O.x, &O.y); for(int i = 0; i < n; ++i) { scanf("%lf%lf", &P[i].x, &P[i].y); } int Max = -2, Min = 2; for(int i = 1; i < n; ++i) { int t = dcmp(xmult(P[i], P[(i+2)%n], P[(i+1)%n])); Max = max(Max, t), Min = min(Min, t); } if(Max+Min != 0) { int flag = 2; for(int i = 0; i < n; ++i) { if(dcmp(disptoseg(O, Line{P[i], P[(i+1)%n]})-r) < 0){ flag--; break; } } for(int i = 0; i < n; ++i) { if(Min == -1){ if(dcmp(xmult(P[(i+1)%n], O, P[i])) != 1) { flag--; break; } } else { if(dcmp(xmult(P[(i+1)%n], O, P[i])) != -1) { flag--; break; } } } if(flag == 2) { printf("PEG WILL FIT\n"); } else { printf("PEG WILL NOT FIT\n"); } } else { printf("HOLE IS ILL-FORMED\n"); } } return 0;}
Poj 1584 a round peg in a ground hole (convex bag Determination & circle determination in Convex bag)