A Round Peg in a Ground Hole
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 6162 |
|
Accepted: 1971 |
题意:按照顺时针(逆时针)顺数给定n个点,判段他们能否构成凸包,再给定一个圆心和半径,判段该圆是否在这个凸包内。
板子题;
Description
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
Source
Mid-Atlantic 2003
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const double eps=1e-6;
const double pi=acos(-1.0);
typedef struct node
{
double x,y;
}point;
int n;
point peg;
double R;
int precision(double x)
{
if(fabs(x)<=eps)
return 0;
return x>0?1:-1;
}
double dotdet(double x1,double y1,double x2,double y2)
{
return x1*x2+y2*y1;
}
double det(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
double cross(point A,point B,point C,point D)
{
return det(B.x-A.x,B.y-A.y,D.x-C.x,D.y-C.y);
}
double Distance(point A,point B)
{
return sqrt(pow(B.x-A.x,2)+pow(B.y-A.y,2));
}
double angle(point A,point B,point P)
{
return acos(dotdet(A.x-P.x,A.y-P.y,B.x-P.x,B.y-P.y)/(Distance(A,P)*Distance(B,P)));
}
bool Iscon(point *vect)
{
int direction=0;
int tmp;
for(int i=0;i<n;i++)
{
tmp=precision(cross(vect[i],vect[i+1],vect[i+1],vect[i+2]));
if(!direction)
direction=tmp;
if(direction*tmp<0)
return 0;
}
return 1;
}
bool IsIn(point *vect)
{
double CircleAngle=0;
for(int i=1;i<=n;i++)
{
if(precision(cross(peg,vect[i],peg,vect[i+1])))
CircleAngle+=angle(vect[i],vect[i+1],peg);
else
CircleAngle-=angle(vect[i],vect[i+1],peg);
}
if(precision(CircleAngle)==0)
return 0;
else if(precision(CircleAngle-pi)==0||precision(CircleAngle+pi)==0)
{
if(precision(R)==0)
return 1;
}
else if(precision(CircleAngle-2*pi)==0||precision(CircleAngle+2*pi)==0)
{
return true;
}
else
{
if(precision(R)==0)
return 1;
}
return 0;
}
bool Isfit(point* vist)
{
int k;
for(int i=0;i<=n;i++)
{
k=precision(fabs(cross(peg,vist[i],peg,vist[i+1]))/Distance(vist[i],vist[i+1])-R);
if(k<0)
return 0;
}
return 1;
}
int main()
{
while(cin>>n&&n>=3)
{
cin>>R>>peg.x>>peg.y;
point* vect=new point[n+3];
for(int i=1;i<=n;i++)
{
cin>>vect[i].x>>vect[i].y;
}
vect[0]=vect[n];vect[n+1]=vect[1];
if(!Iscon(vect))
cout<<"HOLE IS ILL-FORMED"<<endl;
else
{
int flag1=IsIn(vect);
int flag2=Isfit(vect);
if(flag1&&flag2)
cout<<"PEG WILL FIT"<<endl;
else
cout<<"PEG WILL NOT FIT"<<endl;
}
delete vect;
}
return 0;
}