A Round Peg in a Ground Hole
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7250 | Accepted: 2361 |
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).
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.
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
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
射线法判断点是否在多边形内
点在边上要特判
经过点的话 只有这个点为线段交高/低才计入
如果线段与射线平行 不计入贡献
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef double db;
typedef long long ll;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=1010;
struct point
{
db x,y;
friend point operator +(const point &a,const point &b)
{return (point){a.x+b.x,a.y+b.y};}
friend point operator -(const point &a,const point &b)
{return (point){a.x-b.x,a.y-b.y};}
friend db dis(const point &a,const point &b)
{return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
friend db dot(const point &x,const point &y,const point &bas)
{
point a(x-bas),b(y-bas);
return a.x*b.x+a.y*b.y;
}
friend db cross(const point &x,const point &y,const point &bas)
{
point a(x-bas),b(y-bas);
return a.x*b.y-a.y*b.x;
}
}p[N],peg;
struct segment
{
point X,Y;
friend db get_dis(const segment &s)
{
if(dot(peg,s.X,s.Y)*dot(peg,s.Y,s.X)>0)
return fabs(cross(s.X,s.Y,peg))/dis(s.X,s.Y);
return min(dis(peg,s.X),dis(peg,s.Y));
}
}seg[N];
int scnt;
inline void add_seg(int x,int y)
{if(p[x].y>p[y].y) swap(x,y);seg[++scnt].X=p[x];seg[scnt].Y=p[y];}
int n;
db peg_R;
bool peg_in()
{
register int i,tot(0);
db x;
for(i=1;i<=n;++i)
if(peg.y<seg[i].Y.y && peg.y>=seg[i].X.y && seg[i].Y.y-seg[i].X.y>0)
{
x=seg[i].X.x+(seg[i].Y.x-seg[i].X.x)*(peg.y-seg[i].X.y)/(seg[i].Y.y-seg[i].X.y);
if(peg.x==x) return 0;
if(x>=peg.x) tot++;
}
return tot&1;
}
int main()
{
register int i;
while(1)
{
n=read();
if(n<3) break;
scanf("%lf%lf%lf",&peg_R,&peg.x,&peg.y);
for(i=1;i<=n;++i) scanf("%lf%lf",&p[i].x,&p[i].y);
scnt=0;p[n+1]=p[1];
for(i=1;i<=n;++i) add_seg(i,i+1);
bool flag[2]={};flag[0]=flag[1]=0;
for(i=1;i<n;++i)
{
if(cross(p[i],p[i+1],p[i+2])<0) flag[0]=1;
else if(cross(p[i],p[i+1],p[i+2])>0) flag[1]=1;
}
if(flag[0] && flag[1])
{
puts("HOLE IS ILL-FORMED");
continue;
}
if(!peg_in())
{
puts("PEG WILL NOT FIT");
continue;
}
flag[0]=0;
for(i=1;i<=n;++i)
if(get_dis(seg[i])<peg_R)
{
puts("PEG WILL NOT FIT");
flag[0]=1;break;
}
if(!flag[0]) puts("PEG WILL FIT");
}
return 0;
}