A Round Peg in a Ground Hole
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5603 | Accepted: 1788 |
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 (xi+1, yi+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
题意:已知一个多边形的n个顶点坐标,然后再给一个钉子,给定钉子的半径和圆心坐标,首先判断多边形是否为凸多边形,若为凸多边形,再判断钉子是否在到凸多边形内部。
题解:若为凸多边形,则先判断圆心是否在多边形里面,若不在则圆在外部,否则再判断多边形是否存在边到圆心的距离小于r的。
#include<cstring> #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<vector> const double eps=1e-10; #define _sign(x) ((x)>eps?1:((x)<-eps?2:0)) using namespace std; int n; double r; struct Point { double x,y; }; Point peg; vector<Point>vec; double xmulti(Point p1,Point p2,Point p0) { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } ///判断是否是凸多边形 int is_convex() { int i,s[3]= {1,1,1}; for(i=0; i<n&&s[1]|s[2]; i++) { s[_sign(xmulti(vec[(i+1)%n],vec[(i+2)%n],vec[i]))]=0; } return s[1]|s[2]; } ///判断点p是否在多边形里面 int inside(Point q) { int i,s[3]= {1,1,1}; for(i=0; i<n&&s[1]|s[2]; i++) { s[_sign(xmulti(vec[(i+1)%n],q,vec[i]))]=0; } return s[0]&&(s[1]|s[2]); } double dist(Point a,Point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } ///直线交点 Point inter(Point u1,Point u2,Point v1,Point v2) { Point ret=u1; double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x)) /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x)); ret.x+=(u2.x-u1.x)*t; ret.y+=(u2.y-u1.y)*t; return ret; } ///线段交点 Point ptoseg(Point p,Point l1,Point l2) { Point t=p; t.x+=l1.y-l2.y; t.y+=l2.x-l1.x; if(xmulti(l1,t,p)*xmulti(l2,t,p)>eps) return dist(p,l1)<dist(p,l2)?l1:l2; return inter(p,t,l1,l2); } int main() { //freopen("test.in","r",stdin); while(cin>>n) { if(n<3)break; vec.clear(); scanf("%lf%lf%lf",&r,&peg.x,&peg.y); Point it; for(int i=0; i<n; i++) { scanf("%lf%lf",&it.x,&it.y); vec.push_back(it); } if(!is_convex()) {///不是凸多边形 printf("HOLE IS ILL-FORMED\n"); continue; } if(!inside(peg)) {///圆心不在里面 printf("PEG WILL NOT FIT\n"); continue; } bool flag=0; for(int i=0; i<n; i++) { it= ptoseg(peg,vec[i],vec[(i+1)%n]); double R=dist(it,peg); if(R<r){ flag=1; break; } } if(flag)printf("PEG WILL NOT FIT\n"); else printf("PEG WILL FIT\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。