简单计算几何
A Round Peg in a Ground Hole
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4439 | Accepted: 1363 |
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
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
const int maxn=1000;
const double eps=1e-8;
const double pi=acos(-1.0);
inline double sqr(double x)
{
return x*x;
}
int sig(double x)
{
if(fabs(x)<eps) return 0;
if(x>0) return 1;
return -1;
}
struct point
{
double x,y;
point(){};
point(double a,double b):x(a),y(b){}
void input()
{
scanf("%lf%lf",&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 bool operator == (const point &a,const point &b)
{
return sig(a.x-b.x)==0 && sig(a.y-b.y)==0;
}
friend point operator * (const point &a,const double &b)
{
return point(a.x*b,a.y*b);
}
friend point operator * (const double &a,const point &b)
{
return point(a*b.x,a*b.y);
}
friend point operator / (const point &a,const double &b)
{
return point(a.x/b,a.y/b);
}
double norm()
{
return sqrt(sqr(x)+sqr(y));
}
};
double det(const point &a,const point &b)
{
return a.x*b.y-a.y*b.x;
}
double dot(const point &a,const point &b)
{
return a.x*b.x+a.y*b.y;
}
double dist(const point &a,const point &b)
{
return (a-b).norm();
}
struct line
{
point a,b;
line() {}
line(point x,point y):a(x),b(y){}
};
double dis_point_segment(const point p,const point s,const point t)
{
if(sig(dot(p-s,t-s))<0) return (p-s).norm();
if(sig(dot(p-t,s-t))<0) return (p-t).norm();
return fabs(det(s-p,t-p)/dist(s,t));
}
bool PointOnSegment(point p,point s,point t)
{
return sig(det(p-s,t-s))==0 && sig(dot(p-s,p-t))<=0;
}
struct polygon
{
int n;
point a[maxn];
polygon(){}
int judDir()
{
int d;
a[n]=a[0];
for(int i=0;i<n-1;i++)
{
d=sig(det(a[i+1]-a[i],a[i+2]-a[i+1]));
if(d!=0) break;
}
return d;
}
void to_counter_clockwise()
{
for(int i=0;i<(n)/2;i++) swap(a[i],a[n-1-i]);
}
int Point_In(point t)
{
int num=0,d1,d2,k;
a[n]=a[0];
for(int i=0;i<n;i++)
{
if(PointOnSegment(t,a[i],a[i+1])) return 2;
k=sig(det(a[i+1]-a[i],t-a[i]));
d1=sig(a[i].y-t.y);
d2=sig(a[i+1].y-t.y);
if(k>0 && d1<=0 && d2>0) num++;
if(k<0 && d2<=0 && d1>0) num--;
}
return num!=0;
}
bool isConvex()
{
int jud=judDir();
for(int i=0;i<n-1;i++)
{
double k=det(a[i+1]-a[i],a[i+2]-a[i+1]);
int fix=sig(k);
if(fix!=jud&& fix!=0) return false;
}
return true;
}
};
point cri;
polygon pl;
double r;
int n;
int main()
{
while(cin>>n && n>=3)
{
cin>>r;
cri.input();
pl.n=n;
for(int i=0;i<n;i++)
pl.a[i].input();
int jud=pl.judDir();
bool ok=1;
for(int i=0;i<n-1;i++)
{
double k=det(pl.a[i+1]-pl.a[i],pl.a[i+2]-pl.a[i+1]);
int fix=sig(k);
if(fix!=jud&& fix!=0) ok=0;
}
if(!ok)
{
cout<<"HOLE IS ILL-FORMED"<<endl;
continue;
}
if(jud==1) pl.to_counter_clockwise();
if(!pl.Point_In(cri))
{
cout<<"PEG WILL NOT FIT"<<endl;
continue;
}
pl.a[n]=pl.a[0];
double dis=0x3f3f3f3f;
for(int i=0;i<n;i++)
dis=min(dis,dis_point_segment(cri,pl.a[i],pl.a[i+1]));
if(sig(dis-r)>=0) cout<<"PEG WILL FIT"<<endl;
else cout<<"PEG WILL NOT FIT"<<endl;
}
}