# [POJ 1584] A Round Peg in a Ground Hole【计算几何】

• 2017-12-23
• 0
• 0

## Problem:

 Time Limit: 1000MS Memory Limit: 10000K

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

Mid-Atlantic 2003

## Solution:

1. 判断多边形是否为凸。需要按顺时针逆时针各判断一次。
2. 判断圆是否在凸多边形内。先判断圆心是否在多边形内（实现时使用了弧长法[Riks Method]），再判断半径是否都小于等于圆心到各边的距离。

## Code: O(Tv) [180K, 16MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define eps 1e-8
#define dmax 1e10
#define sqr(x) ((x) * (x))
using namespace std;

inline int dsgn(const double &x){
if(fabs(x) <= eps) return 0;
return (x < 0) ? -1 : 1;
}

double r2;

struct Polygon;

#define Vector Point
struct Point{
double x, y;

Point() {}
Point(double x, double y): x(x), y(y) {}

inline Vector operator - (const Point &p2) const {return Vector(x - p2.x, y - p2.y);}

inline bool inPolygon(const Polygon &poly);

inline double toSegment2(const Point &p1, const Point &p2);
} peg;

inline double sqrlen(const Vector &vec) {return sqr(vec.x) + sqr(vec.y);}

inline double Dot(const Vector &vec1, const Vector &vec2) {return vec1.x * vec2.x + vec1.y * vec2.y;}

inline double Cross(const Vector &vec1, const Vector &vec2) {return vec1.x * vec2.y - vec1.y * vec2.x;}

struct Polygon{
Point v;
int vnum;

inline bool isConvex(){
bool iscv = true;
int dpre = vnum - 1, pre = vnum;
for(register int i = 1; i <= vnum; i++){
if(dsgn(Cross(v[i] - v[dpre], v[pre] - v[dpre])) < 0){
iscv = 0;
break;
}
dpre = pre, pre = i;
}  // Test if all the turnings are clockwise
if(iscv) return 1;
iscv = true;
dpre = vnum - 1, pre = vnum;
for(register int i = 1; i <= vnum; i++){
if(dsgn(Cross(v[i] - v[dpre], v[pre] - v[dpre])) > 0){
iscv = 0;
break;
}
dpre = pre, pre = i;
}  // Test if all the turnings are counter-clockwise
return iscv;
}
} hole;

inline bool Point::inPolygon(const Polygon &poly){
int arc = 0, pre = poly.vnum;
for(register int i = 1; i <= poly.vnum; i++){
double crs = Cross(poly.v[pre] - *this, poly.v[i] - *this);
if(dsgn(crs) == 0) return 0; // The point is on the edge of the polygon, so it'll never fit
double dxpre = poly.v[pre].x - x, dypre = poly.v[pre].y - y;
double dxi = poly.v[i].x - x, dyi = poly.v[i].y - y;
int quarpre = dsgn(dxpre) > 0 ? (dsgn(dypre) > 0 ? 0 : 3) : (dsgn(dypre) > 0 ? 1 : 2);
int quari = dsgn(dxi) > 0 ? (dsgn(dyi) > 0 ? 0 : 3) : (dsgn(dyi) > 0 ? 1 : 2);
int dquar = (quari - quarpre + 4) % 4;
if(dquar == 1) arc++;
else if(dquar == 3) arc--;
else if(dquar == 2) dsgn(crs) > 0 ? (arc += 2) : (arc -= 2);
pre = i;
}
return (arc != 0);
}  // Riks Method

inline double Point::toSegment2(const Point &p1, const Point &p2){
double seglen = sqrt(sqrlen(p2 - p1));
if(dsgn(seglen) == 0) return sqrlen(*this - p1);
double shadowlen = Dot(*this - p1, p2 - p1) / seglen;
if(dsgn(shadowlen) <= 0) return sqrlen(*this - p1);
if(dsgn(shadowlen - seglen) >= 0) return sqrlen(*this - p2);
return sqrlen(*this - p1) - sqr(shadowlen);
}

int main(){
while(scanf("%d", &hole.vnum) && hole.vnum >= 3){
scanf("%lf%lf%lf", &r2, &peg.x, &peg.y), r2 = sqr(r2);
for(register int i = 1; i <= hole.vnum; i++) scanf("%lf%lf", &hole.v[i].x, &hole.v[i].y);
if(!hole.isConvex()){
puts("HOLE IS ILL-FORMED");
continue;
}
if(!peg.inPolygon(hole)){
puts("PEG WILL NOT FIT");
continue;
}
int pre = hole.vnum;
bool isfit = 1;
for(register int i = 1; i <= hole.vnum; i++){
double dis2 = peg.toSegment2(hole.v[i], hole.v[pre]);
if(dsgn(r2 - dis2) > 0){
isfit = 0;
break;
}
pre = i;
}
if(isfit) puts("PEG WILL FIT");
else puts("PEG WILL NOT FIT");
}
return 0;
}
```

#### 评论

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai