POJ 1584 A Round Peg in a Ground Hole 判断凸多边形 点到线段距离 点在多边形内
首先判断是不是凸多边形
然后判断圆是否在凸多边形内
不知道给出的点是顺时针还是逆时针,所以用判断是否在多边形内的模板,不用是否在凸多边形内的模板
POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const double eps=1e-8;
const double PI = acos(-1.0); int sgn(double x)
{
if(fabs(x) < eps) return 0;
return x < 0 ? -1:1;
} 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 p,q;
Line() {};
Line(Point _p,Point _q)
{
p = _p,q = _q;
}
}; //*两点间距离
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
} //*判断凸多边形
//允许共线边
//点可以是顺时针给出也可以是逆时针给出
//点的编号0~n-1
bool isconvex(Point poly[],int n)
{
bool s[3];
memset(s,false,sizeof(s));
for(int i = 0; i < n; i++)
{
s[sgn( (poly[(i+1)%n]-poly[i])^(poly[(i+2)%n]-poly[i]) )+1] = true;
if(s[0] && s[2])return false;
}
return true;
} //*点到线段的距离
//返回点到线段最近的点
Point NearestPointToLineSeg(Point P,Line L)
{
Point result;
double t = ((P-L.p)*(L.q-L.p))/((L.q-L.p)*(L.q-L.p));
if(t >= 0 && t <= 1)
{
result.x = L.p.x + (L.q.x - L.p.x)*t;
result.y = L.p.y + (L.q.y - L.p.y)*t;
}
else
{
result = dist(P,L.p) < dist(P,L.q)? L.p:L.q;
}
return result;
} //*判断点在线段上
bool OnSeg(Point P,Line L)
{
return
sgn((L.p-P)^(L.q-P)) == 0 &&
sgn((P.x - L.p.x) * (P.x - L.q.x)) <= 0 &&
sgn((P.y - L.p.y) * (P.y - L.q.y)) <= 0;
} //*判断点在凸多边形内
//点形成一个凸包,而且按逆时针排序(如果是顺时针把里面的<0改为>0)
//点的编号:0~n-1
//返回值:
//-1:点在凸多边形外
//0:点在凸多边形边界上
//1:点在凸多边形内
int inConvexPoly(Point a,Point p[],int n)
{
for(int i = 0; i < n; i++)
{
if(sgn((p[i]-a)^(p[(i+1)%n]-a)) < 0)return -1;
else if(OnSeg(a,Line(p[i],p[(i+1)%n])))return 0;
}
return 1;
} //*判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.p.x,l1.q.x) >= min(l2.p.x,l2.q.x) &&
max(l2.p.x,l2.q.x) >= min(l1.p.x,l1.q.x) &&
max(l1.p.y,l1.q.y) >= min(l2.p.y,l2.q.y) &&
max(l2.p.y,l2.q.y) >= min(l1.p.y,l1.q.y) &&
sgn((l2.p-l1.q)^(l1.p-l1.q))*sgn((l2.q-l1.q)^(l1.p-l1.q)) <= 0 &&
sgn((l1.p-l2.q)^(l2.p-l2.q))*sgn((l1.q-l2.q)^(l2.p-l2.q)) <= 0;
} //*判断点在任意多边形内
//射线法,poly[]的顶点数要大于等于3,点的编号0~n-1
//返回值
//-1:点在多边形外
//0:点在多边形边界上
//1:点在多边形内
int inPoly(Point p,Point poly[],int n)
{
int cnt;
Line ray,side;
cnt = 0;
ray.p = p;
ray.q.y = p.y;
ray.q.x = -100000000000.0;//-INF,注意取值防止越界
for(int i = 0; i < n; i++)
{
side.p = poly[i];
side.q = poly[(i+1)%n];
if(OnSeg(p,side))return 0;
//如果平行轴则不考虑
if(sgn(side.p.y - side.q.y) == 0)
continue;
if(OnSeg(side.p,ray))
{
if(sgn(side.p.y - side.q.y) > 0)cnt++;
}
else if(OnSeg(side.q,ray))
{
if(sgn(side.q.y - side.p.y) > 0)cnt++;
}
else if(inter(ray,side)) cnt++;
}
return cnt % 2 ? 1:-1;
} Point pot[105],peg;
double rad; int main()
{
// freopen("in.txt","r",stdin);
int n;
while(~scanf("%d",&n))
{
if(n<3) break;
scanf("%lf",&rad);
peg.input();
for(int i=0;i<n;i++)
pot[i].input();
if(!isconvex(pot,n))
{
puts("HOLE IS ILL-FORMED");
continue;
}
if(inPoly(peg,pot,n)<0)
{
puts("PEG WILL NOT FIT");
continue;
}
bool flag=true;
for(int i=0;i<n-1;i++)
{
if( sgn(dist(peg,NearestPointToLineSeg(peg,Line(pot[i],pot[(i+1)%n])))-rad)<0 )
{
flag=false;
break;
}
}
puts(flag? "PEG WILL FIT":"PEG WILL NOT FIT");
}
return 0;
}
POJ 1584 A Round Peg in a Ground Hole 判断凸多边形 点到线段距离 点在多边形内的更多相关文章
- POJ 1584 A Round Peg in a Ground Hole 判断凸多边形,判断点在凸多边形内
A Round Peg in a Ground Hole Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5456 Acc ...
- POJ 1584 A Round Peg in a Ground Hole[判断凸包 点在多边形内]
A Round Peg in a Ground Hole Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6682 Acc ...
- POJ 1584 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】
链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)
A Round Peg in a Ground Hole Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4438 Acc ...
- POJ - 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)
http://poj.org/problem?id=1584 题意 按照顺时针或逆时针方向输入一个n边形的顶点坐标集,先判断这个n边形是否为凸包. 再给定一个圆形(圆心坐标和半径),判断这个圆是否完全 ...
- POJ 1584 A Round Peg in a Ground Hole --判定点在形内形外形上
题意: 给一个圆和一个多边形,多边形点可能按顺时针给出,也可能按逆时针给出,先判断多边形是否为凸包,再判断圆是否在凸包内. 解法: 先判是否为凸包,沿着i=0~n,先得出初始方向dir,dir=1为逆 ...
- 简单几何(点的位置) POJ 1584 A Round Peg in a Ground Hole
题目传送门 题意:判断给定的多边形是否为凸的,peg(pig?)是否在多边形内,且以其为圆心的圆不超出多边形(擦着边也不行). 分析:判断凸多边形就用凸包,看看点集的个数是否为n.在多边形内用叉积方向 ...
- POJ 1584 A Round Peg in a Ground Hole
先判断是不是N多边形,求一下凸包,如果所有点都用上了,那么就是凸多边形 判断圆是否在多边形内, 先排除圆心在多边形外的情况 剩下的情况可以利用圆心到每条边的最短距离与半径的大小来判断 #include ...
- POJ 1518 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】
链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
随机推荐
- 查看linux系统是多少位,使用 getconf LONG_BIT
查看linux系统是多少位,使用 getconf LONG_BIT echo $HOSTTYPE
- openstack宿主机故障,虚拟实例恢复
前言: 因为机房服务器运行不稳定的原因导致计算节点挂掉,然后上面的Centos7虚拟机在迁移之后开机报错.这个解决方法同样适用于其它操作系统的虚拟机.基于镜像创建的虚拟机实例. I/O error, ...
- python基础之面向对象(三))(实战:烤地瓜(SweetPotato))
一.分析"烤地瓜"的属性和方法 示例属性如下: cookedLevel : 这是数字:0~3表示还是生的,超过3表示半生不熟,超过5表示已经烤好了,超过8表示已经烤成木炭了!我们的 ...
- IDEA 常用快捷键列表【建议收藏】
编辑代码 快捷键 说明 Alt+Enter 导入包.自动变量命名等(万能快捷键) Ctrl+X 删除行 Ctrl+Y 删除当前行 Ctrl+D 复制行 Alt+Shift+Up/Down或Ctrl+S ...
- docker0详解
docker0:https://blog.csdn.net/kubailing/article/details/87936501 veth pair详解:https://www.cnblogs.com ...
- Python+Selenium - 下拉列表处理
下拉列表分两种:select下拉表和非select下拉表. 1.select下拉列表 如下图元素代码展示 可用Select类处理 from selenium.webdriver.support.sel ...
- ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁
作者:Grey 原文地址: ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁 前置知识 完成ZooKeeper集群搭建以及熟悉ZooKeeperAPI基本使用 需求 当多个进 ...
- MXNet 图优化与算子融合
MXNet 图优化与算子融合Graph Optimization and Quantization based on subgraph and MKL-DNN Purpose MKL-DNN引入了两个 ...
- nvGRAPH三角形计数和遍历示例
nvGRAPH三角形计数和遍历示例 #include " stdlib.h" #include" inttypes.h" #include" stdi ...
- 深入理解java虚拟机笔记Chapter4
JDK命令行工具 其中的重中之重是 jstat 命令!而它最常用的参数就是 -gcutil,使用格式如下: jstat -gcutil [pid] [intervel] [count] 输出如下 D: ...