POJ - 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)

时间:2022-08-12 04:24:04

http://poj.org/problem?id=1584

题意

按照顺时针或逆时针方向输入一个n边形的顶点坐标集,先判断这个n边形是否为凸包。

再给定一个圆形(圆心坐标和半径),判断这个圆是否完全在n边形内部。

分析

1.判断给出了多边形是不是凸多边形。

2.判断圆包含在凸多边形中:一定要保证圆心在凸多边形里面。然后判断圆心到每条线段的距离要大于等于半径。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h> using namespace std; const double eps = 1e-;
const double PI = acos(-1.0);
int sgn(double x)
{
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x = _x;y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//叉积
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
void input()
{
scanf("%lf%lf",&x,&y);
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
};
//*两点间距离
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
//判断凸多边形
//允许共线边
//点可以是顺时针给出也可以是逆时针给出
//点的编号1~n-1
bool isconvex(Point poly[],int n)
{
bool s[];
memset(s,false,sizeof(s));
for(int i = ;i < n;i++)
{
s[sgn( (poly[(i+)%n]-poly[i])^(poly[(i+)%n]-poly[i]) )+] = true;
if(s[] && s[])return false;
}
return true;
}
//点到线段的距离
//返回点到线段最近的点
Point NearestPointToLineSeg(Point P,Line L)
{
Point result;
double t = ((P-L.s)*(L.e-L.s))/((L.e-L.s)*(L.e-L.s));
if(t >= && t <= )
{
result.x = L.s.x + (L.e.x - L.s.x)*t;
result.y = L.s.y + (L.e.y - L.s.y)*t;
}
else
{
if(dist(P,L.s) < dist(P,L.e))
result = L.s;
else result = L.e;
}
return result;
}
//*判断点在线段上
bool OnSeg(Point P,Line L)
{
return
sgn((L.s-P)^(L.e-P)) == &&
sgn((P.x - L.s.x) * (P.x - L.e.x)) <= &&
sgn((P.y - L.s.y) * (P.y - L.e.y)) <= ;
}
//*判断点在凸多边形内
//点形成一个凸包,而且按逆时针排序(如果是顺时针把里面的<0改为>0)
//点的编号:0~n-1
//返回值:
//-1:点在凸多边形外
//0:点在凸多边形边界上
//1:点在凸多边形内
int inConvexPoly(Point a,Point p[],int n)
{
for(int i = ;i < n;i++)
{
if(sgn((p[i]-a)^(p[(i+)%n]-a)) < )return -;
else if(OnSeg(a,Line(p[i],p[(i+)%n])))return ;
}
return ;
}
//*判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= ;
}
//*判断点在任意多边形内
//射线法,poly[]的顶点数要大于等于3,点的编号0~n-1
//返回值
//-1:点在凸多边形外
//0:点在凸多边形边界上
//1:点在凸多边形内
int inPoly(Point p,Point poly[],int n)
{
int cnt;
Line ray,side;
cnt = ;
ray.s = p;
ray.e.y = p.y;
ray.e.x = -100000000000.0;//-INF,注意取值防止越界 for(int i = ;i < n;i++)
{
side.s = poly[i];
side.e = poly[(i+)%n]; if(OnSeg(p,side))return ; //如果平行轴则不考虑
if(sgn(side.s.y - side.e.y) == )
continue; if(OnSeg(side.s,ray))
{
if(sgn(side.s.y - side.e.y) > )cnt++;
}
else if(OnSeg(side.e,ray))
{
if(sgn(side.e.y - side.s.y) > )cnt++;
}
else if(inter(ray,side))
cnt++;
}
if(cnt % == )return ;
else return -;
}
Point p[];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
double R,x,y;
while(scanf("%d",&n) == )
{
if(n < )break;
scanf("%lf%lf%lf",&R,&x,&y);
for(int i = ;i < n;i++)
p[i].input();
if(!isconvex(p,n))
{
printf("HOLE IS ILL-FORMED\n");
continue;
}
Point P = Point(x,y);
if(inPoly(P,p,n) < )
{
printf("PEG WILL NOT FIT\n");
continue;
}
bool flag = true;
for(int i = ;i < n;i++)
{
if(sgn(dist(P,NearestPointToLineSeg(P,Line(p[i],p[(i+)%n]))) - R) < )
{
flag = false;
break;
}
}
if(flag)printf("PEG WILL FIT\n");
else printf("PEG WILL NOT FIT\n");
}
return ;
}