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 ...
随机推荐
- VulnHub系列(一)DC-1
环境 kali linux 和 DC-1 都是搭建在VMware上的虚拟机,都是NAT模式. 主机发现 NAT模式下虚拟机没有被分配真实的ip地址,他们通过共享宿主机的ip地址访问互联网.我们可以通过 ...
- IDEA Git 操作常见错误处理
使用 IDEA 的 git 进行操作时报错 更新报错 Git Pull Failed: refusing to merge unrelated histories 提交报错 Push rejected ...
- MyBaits 全局配置文件(mybatis-config.xml)
什么是 MyBatis 全局配置文件 MyBatis 全局配置文件包含影响 MyBatis 框架正常使用的功能设置和属性信息. 它的作用好比手机里的设置图标,点击这个图标就可以帮助我们查看手机的属性信 ...
- Python3.x 基础练习题100例(91-100)
练习91: 题目: 时间函数举例1. 程序: if __name__ == '__main__': import time print (time.ctime(time.time())) print ...
- Git 系列教程(14)- 远程分支
远程分支 远程引用是对远程仓库的引用(指针),包括分支.标签等等 你可以通过 git ls-remote <remote> 来显式地获得远程引用的完整列表 polo@B-J5D1MD6R- ...
- 对狂神的shiro的学习总结
1.shiro的10分钟快速开始 导入依赖 新建一个普通的maven项目,然后new一个hello-shiro(moudle)作为第一个测试项目 具体框架如下: 导入对应的依赖在pom.xml文件里 ...
- Ubuntu 20.04 Docker 安装并配置
前言 Docker 的使用能极大地方便我们的开发,减少环境搭建,依赖安装等繁琐且容易出错的问题. 安装 Docker Ubuntu 20.04 官方 apt 源中就有 Docker,我们可以直接通过 ...
- camera中LENS和SENSOR的CRA是如何搭配的?
camera中LENS和SENSOR的CRA是如何搭配的? camera中,lens和sensor的搭配是非常关键的问题.但这两者是如何搭配的呢? 一般在Sensor data sheet中会附有全视 ...
- 华为MDC自动驾驶
华为MDC自动驾驶 智能驾驶汽车中,包含四个核心子系统:传感器.计算平台.执行器与应用算法,华为MDC( Mobile Data Center: 移动数据中心)定位为智能驾驶的计算平台.此平台集成了华 ...
- Caffe实现概述
Caffe实现概述 目录 一.caffe配置文件介绍 二.标准层的定义 三.网络微调技巧 四.Linux脚本使用及LMDB文件生成 五.带你设计一个Caffe网络,用于分类任务 一.caffe配置文件 ...