题目链接:https://vjudge.net/problem/POJ-1584
The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces of wood are attached to one another using a wooden peg that fits into pre-cut holes in each piece to be attached. The pegs have a circular cross-section and so are intended to fit inside a round hole.
A recent factory run of computer desks were flawed when an automatic grinding machine was mis-programmed. The result is an irregularly shaped hole in one piece that, instead of the expected circular shape, is actually an irregular polygon. You need to figure out whether the desks need to be scrapped or if they can be salvaged by filling a part of the hole with a mixture of wood shavings and glue.
There are two concerns. First, if the hole contains any protrusions (i.e., if there exist any two interior points in the hole that, if connected by a line segment, that segment would cross one or more edges of the hole), then the filled-in-hole would not be structurally sound enough to support the peg under normal stress as the furniture is used. Second, assuming the hole is appropriately shaped, it must be big enough to allow insertion of the peg. Since the hole in this piece of wood must match up with a corresponding hole in other pieces, the precise location where the peg must fit is known.
Write a program to accept descriptions of pegs and polygonal holes and determine if the hole is ill-formed and, if not, whether the peg will fit at the desired location. Each hole is described as a polygon with vertices (x1, y1), (x2, y2), . . . , (xn, yn). The edges of the polygon are (xi, yi) to (x i+1, y i+1) for i = 1 . . . n − 1 and (xn, yn) to (x1, y1).
Input
Input consists of a series of piece descriptions. Each piece description consists of the following data:
Line 1 < nVertices > < pegRadius > < pegX > < pegY >
number of vertices in polygon, n (integer)
radius of peg (real)
X and Y position of peg (real)
n Lines < vertexX > < vertexY >
On a line for each vertex, listed in order, the X and Y position of vertex The end of input is indicated by a number of polygon vertices less than 3.
Output
For each piece description, print a single line containing the string:
HOLE IS ILL-FORMED if the hole contains protrusions
PEG WILL FIT if the hole contains no protrusions and the peg fits in the hole at the indicated position
PEG WILL NOT FIT if the hole contains no protrusions but the peg will not fit in the hole at the indicated position
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
题意理解:
输入n个点,圆的半径以及圆的坐标X,Y。然后是每个点的x,y坐标.
判断输入的点是不是能够组成凸包,不能的话输出 HOLE IS ILL-FORMED,判断圆是否在凸包内.如果在的话输出PEG WILL FIT.否则输出 PEG WILL NOT FIT。
直接按照题意模拟就行了,不过板子要足够好。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
struct Point
{
double x,y;
Point(double x = 0, double y = 0):x(x),y(y){}
} p[2005];
typedef Point Vector;
Point c;
double r;
int n;
Vector operator - (Point A, Point B){
return Vector(A.x-B.x, A.y-B.y);
}
double Dot(Point a,Point b,Point c){
return (a.x-c.x)*(b.x-c.x)+(a.y-c.y)*(b.y-c.y);
}
double Cross(Point a,Point b,Point c)
{
return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
int isCross(double k)
{
if(k<0) return 1;
return 0;
}
bool isConvex()//判断是否是凸包
{
p[n++]=p[0];
p[n++]=p[1];
int dir = isCross(Cross(p[1],p[2],p[0]));
for(int i=3; i<n; i++)
if(isCross(Cross(p[i-1],p[i],p[i-2]))!=dir)
return false;
return true;
}
bool pointInPolygon(int n,double x,double y)//判断圆心是否在多边形内
{
int i,j=n-1;
bool oddNodes=0;
for (i=0; i<n; i++)
{
if((p[i].y< y && p[j].y>=y|| p[j].y<y && p[i].y>=y) && (p[i].x<=x||p[j].x<=x))
{
oddNodes^=(p[i].x+(y-p[i].y)/(p[j].y-p[i].y)*(p[j].x-p[i].x)<x);
}
j=i;
}
return oddNodes;
}
double dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double GetDist(Point a,Point b,Point c){
if(dis(a,b)<eps) return dis(b,c);
if(Dot(b,c,a)<-eps) return dis(a,c);
if(Dot(a,c,b)<-eps) return dis(b,c);
return fabs(Cross(b,c,a)/dis(a,b));
}
bool circleInConvex(double x,double y,double r){//判断圆是否被包含在多边形内
Point po;
po.x = x,po.y = y;
p[n]=p[0];
for(int i=0;i<n;i++){
if(r>GetDist(p[i],p[i+1],po)){
return false;
}
}
return true;
}
int main()
{
while(~scanf("%d",&n)&&n>=3)
{
scanf("%lf%lf%lf",&r,&c.x,&c.y);
for(int i=0; i<n; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
if(!isConvex())
printf("HOLE IS ILL-FORMED\n");
else if(!circleInConvex(c.x,c.y,r)||!pointInPolygon(n,c.x,c.y))
printf("PEG WILL NOT FIT\n");
else
printf("PEG WILL FIT\n");
}
return 0;
}