A Round Peg in a Ground Hole POJ1584(判断凸包+判断点在多边形内+点到直线距离)

A Round Peg in a Ground HolePOJ - 1584

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
分析:
本问题分为几个子问题:

1.判断多边形是不是凸包

按顺序依次取相邻三点,叉乘,如果符号都相同则是凸包;如果叉乘结果是0,忽略这一次;注意有一种可能,有三个点,三点共线,但是这种情况根据题意似乎不太可能出现

2.判断点在不在多边形里面

对点p,按顺序取相邻两点,判断所有叉乘的符号是否相同;如果有不同的,不在多边形内;如果等于零,那么在边上,由于后面还要看圆的情况,圆心在边上的圆一定不满足条件,在本题中也把这种情况排除掉

3.判断圆在不在多边形里面(实际上就是求点到直线距离)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const double eps=1e-8;
int dcmp(double x){
	if(abs(x)<eps)return 0;
	if(x<0)return -1;
	if(x>0)return 1;
}
struct Vector{
	double x,y;
	Vector(){}
	Vector(double x,double y):x(x),y(y){}
};
typedef Vector point;
Vector operator + (Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator - (Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator * (Vector a,Vector b){return Vector(a.x*b.x,a.y*b.y);}
Vector operator / (Vector a,Vector b){return Vector(a.x/b.x,a.y/b.y);}
double dot(Vector a,Vector b){
	return a.x*b.x+a.y*b.y;
}
double cross(Vector a,Vector b){
	return a.x*b.y-a.y*b.x;
}
double len(Vector a){
	return sqrt(dot(a,a));
}
double dis_p_l(point a,point p1,point p2){\
	return abs(cross(a-p1,p2-p1))/len(p2-p1);
}
point ps[110];
int main(){
	int n;
	double R,RX,RY;
	cin>>n;
	while(n>=3){
		cin>>R>>RX>>RY;
		point PR(RX,RY);
		for(int i=0;i<n;i++){
			cin>>ps[i].x>>ps[i].y;
		}
		int flag=2;
		//判断是否是凸包 
		int k=0,m;
		for(int i=0;i<n;i++){
			point p1=ps[i],p2=ps[(i+1)%n],p3=ps[(i+2)%n];
			if(k==0){
				k=dcmp(cross(p2-p1,p3-p2));
			}
			else{
				m=dcmp(cross(p2-p1,p3-p2));
				if(m==0)continue;
				if(m!=k){
					flag=1; 
					break;
				} 
			}
		}
		if(flag==1){
			cout<<"HOLE IS ILL-FORMED"<<endl;
			cin>>n;
			continue;
		}
		//判断是否点在多边形内 
		k=0;
		for(int i=0;i<n;i++){
			point p1=ps[i],p2=ps[(i+1)%n];
			int t=dcmp(cross(p1-PR,p2-PR));
			//cout<<"t:"<<t<<endl;
			//cout<<"m:"<<m<<endl;
			if(t==0){
				flag=3;
				break;
			}
			if(k==0)k=t;
			else if(t!=k){
				flag=3;
				break;
			}
		}
		if(flag==3){
			cout<<"PEG WILL NOT FIT"<<endl;
			cin>>n;
			continue;
		}
		//判断是否圆在多边形内 
		for(int i=0;i<n;i++){
			point p1=ps[i],p2=ps[(i+1)%n];
			if(R>dis_p_l(PR,p1,p2)){
				flag=3;
				break;
			}
		}
		if(flag==2)cout<<"PEG WILL FIT"<<endl;
		else if(flag==3)cout<<"PEG WILL NOT FIT"<<endl;
		cin>>n;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41333528/article/details/80543154
今日推荐