poj1584 A round peg in a ground hole【计算几何】

时间:2023-03-10 05:30:06
poj1584 A round peg in a ground hole【计算几何】

含【判断凸包】,【判断点在多边形内】,【判断圆在多边形内】模板 

凸包:即凸多边形

用不严谨的话来讲,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边形,它能包含点集中所有的点。

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

题意:

给定n个点 这n个点组成一个多边形

给定一个peg的坐标和半径

首先判断这个多边形是不是凸多边形 若不是 输出“HOLE IS ILL-FORMED”

否则判断peg和多边形的关系 若peg所代表的圆在多边形内部输出“PEG WILL FIT”

否则输出“PEG WILL NOT FIT”

思路:

首先将n个点构造成封闭图形,判断是不是一个凸包

  求连续两条边的叉乘,如果正负号与之前的出现了不同,说明不是凸包

再判断圆心与多边形的关系

  设圆心为P,逐条枚举n边形的边AB,利用
  poj1584 A round peg in a ground hole【计算几何】

  计算PA和PB的夹角,最后求和得到的就是环顾角。

  (1)       圆心在多边形内部时,环顾角=±360

  (2)       圆心在多边形外部时,环顾角=0

  (3)       圆心在多边形边上时(不包括顶点),环顾角=±180

  (4)       圆心在多边形顶点时,环顾角为(0,360)之间的任意角,其实就是圆心所在的顶点的两条邻接边的夹角。

最后判断整个圆是否在多边形内部

  只需要求出圆心到边的最短距离 若大于半径则在多边形内

  设圆心为P,逐条枚举n边形的边AB,利用poj1584 A round peg in a ground hole【计算几何】得到△PAB的面积,

  再根据公式S=0.5*|AB|*h,可以得到poj1584 A round peg in a ground hole【计算几何】

  枚举所有h与圆的半径R比对,只要所有的边都有h - R>=0,则说明圆在多边形内

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h> using namespace std;
typedef long long int LL; const double eps = 1e-;
const double pi = 3.141592654;
int n;
double radius;
struct point{
double x, y;
}peg; int precision(double x)
{
if(fabs(x) <= eps){
return ;
}
return x > ? : -;
} double dotdet(double x1, double y1, double x2, double y2)
{
return x1 * x2 + y1 * y2;
} 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 distant(point a, point b)
{
return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y));
} 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) / (distant(a, p) * distant(b, p)));
} bool isconvex(point *vectex)
{
int direction = ;
//1, 逆时针;-1, 顺时针
for(int i = ; i < n; i++){
int temp = precision(cross(vectex[i], vectex[i + ], vectex[i + ], vectex[i + ])); if(!direction){
direction = temp;
}
if(direction * temp < ){
return false;
}
}
return true;
} bool is_in(point *vectex)
{
double circleAngle = 0.0;
for(int i = ; i <= n; i++){
if(precision(cross(peg, vectex[i], peg, vectex[i + ])) >= ){
circleAngle += angle(vectex[i], vectex[i + ], peg);
}
else{
circleAngle -= angle(vectex[i], vectex[i + ], peg);
}
} if(precision(circleAngle) == ){
return false;
//peg在多边形外部
}
else if(precision(circleAngle - pi) == || precision(circleAngle + pi) == ){
//peg在多边形边上
if(precision(radius) == ){
return true;
}
}
else if(precision(circleAngle - * pi) == || precision(circleAngle + * pi) == ){
return true;
}
else{
//peg在多边形顶点上
if(precision(radius) == ){
return true;
}
}
return false;
} bool isfit(point *vectex)
{
for(int i = ; i <= n; i++){
int k = precision(fabs(cross(peg, vectex[i], peg, vectex[i + ]) / distant(vectex[i], vectex[i + ])) - radius);
if(k < ){
return false;
}
}
return true;
} int main()
{
while(scanf("%d", &n) != EOF && n >= ){
cin>> radius >> peg.x >> peg.y;
point *vectex = new point[n + ]; for(int i = ; i <= n; i++){
cin>>vectex[i].x >> vectex[i].y;
} //构成封闭多边形
vectex[].x = vectex[n].x;
vectex[].y = vectex[n].y;
vectex[n + ].x = vectex[].x;
vectex[n + ].y = vectex[].y; if(!isconvex(vectex)){
cout<<"HOLE IS ILL-FORMED"<<endl;
}
else{
bool flag1 = is_in(vectex);
bool flag2 = isfit(vectex); if(flag1 && flag2){
cout<<"PEG WILL FIT"<<endl;
}
else{
cout<<"PEG WILL NOT FIT"<<endl;
}
}
delete vectex;
}
return ;
}