A Round Peg in a Ground Hole - POJ 1584 (判断凸多边形&判断点在多边形内&判断圆在多边形内)
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std; const int MAXN = 1e3+;
const double EPS = 1e-;
const double oo = 1e10+; struct Point
{
double x, y;
Point(double x=, double y=):x(x),y(y){}
Point operator - (const Point &tmp)const{
return Point(x-tmp.x, y-tmp.y);
}
double operator ^(const Point &tmp)const{
return x*tmp.y - y*tmp.x;
}
double operator *(const Point &tmp)const{
return x*tmp.x + y*tmp.y;
}
};
double Dist(Point a, Point b)
{///两点间的距离
return sqrt((a-b)*(a-b));
}
int Sign(double t)
{
if(t > EPS)return ;
if(fabs(t) < EPS)return ;
return -;///负数
}
struct Segment
{
Point S, E;
Segment(Point S=, Point E=):S(S), E(E){}
bool OnSeg(const Point &p)
{///点是否在线段上
if(Sign( (S-E)^(p-E) ) == )///共线
if(Sign( (p.x-S.x)*(p.x-E.x) ) <= )///位于线段的中间或者端点
if(Sign( (p.y-S.y)*(p.y-E.y) ) <= )
return true;
return false;
}
bool Inter(const Segment &tmp)
{///只考虑完全相交的情况
return Sign((S-E)^(tmp.S-E)) * Sign((S-E)^(tmp.E-E)) == -;
}
Point NearPoint(const Point &p)
{///点到线段最近的点
Point res;
double r = ((E-S)*(p-S)) / ((E-S)*(E-S)); if(r > EPS && (1.0 - r) > EPS )
{///点在线段的投影在线段上
res.x = S.x + r * (E.x-S.x);
res.y = S.y + r * (E.y-S.y);
}
else
{///求离最近的端点
if(Dist(p, S) < Dist(p, E))
res = S;
else
res = E;
} return res;
}
};
struct Poly
{
int N;
Point vertex[MAXN]; bool IsConvex()
{///判断是否是凸多边形,可以共线
int vis[] = {};
for(int i=; i<N; i++)
{///如果同时出现整数和负数,说明存在凹的
int k = Sign((vertex[(i+)%N]-vertex[i])^(vertex[(i+)%N]-vertex[i]));
vis[k+] = ; if(vis[] && vis[])
return false;
}
return true;
}
int InPoly(const Point &Q)
{///判断点Q是否在多边形内,射线法,奇数在内,偶数在外
///在圆上返回0, 圆外-1, 圆内 1
Segment ray(Point(-oo, Q.y), Q);///构造射线的最远处
int cnt=;///统计相交的边数 for(int i=; i<N; i++)
{
Segment edge(vertex[i], vertex[(i+)%N]); if(edge.OnSeg(Q) == true)
return ;///点在边上 if(ray.OnSeg(vertex[i]) == true)
{///如果相交连接点,那么取y值小的点
if(vertex[(i+)%N].y - vertex[i].y > EPS)
cnt++;
}
else if(ray.OnSeg(vertex[(i+)%N]) == true)
{
if(vertex[i].y - vertex[(i+)%N].y > EPS)
cnt++;
}
else if(ray.Inter(edge) && edge.Inter(ray))
cnt++;
} if(cnt % )
return ;
else
return -;
}
};
struct Circle
{
Point center;///圆心
double R;///半径
}; bool Find(Poly &a, Circle &c)
{///判断圆是否在多边形内
if(a.InPoly(c.center) == -)
return false;///如果圆心在多边形外面 for(int i=; i<a.N; i++)
{
Segment edge(a.vertex[i], a.vertex[(i+)%a.N]);
double len = Dist(c.center, edge.NearPoint(c.center)); if(Sign(len-c.R) < )
return false;
} return true;
} int main()
{
Poly a;///定义多边形
Circle c;///定义圆 while(scanf("%d", &a.N) != EOF && a.N > )
{
scanf("%lf%lf%lf", &c.R, &c.center.x, &c.center.y);
for(int i=; i<a.N; i++)
scanf("%lf%lf", &a.vertex[i].x, &a.vertex[i].y); if(a.IsConvex() == false)
printf("HOLE IS ILL-FORMED\n");
else if(Find(a, c) == false)
printf("PEG WILL NOT FIT\n");
else
printf("PEG WILL FIT\n");
} return ;
}
A Round Peg in a Ground Hole - POJ 1584 (判断凸多边形&判断点在多边形内&判断圆在多边形内)的更多相关文章
- 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 ...
- 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 判断凸多边形 点到线段距离 点在多边形内
首先判断是不是凸多边形 然后判断圆是否在凸多边形内 不知道给出的点是顺时针还是逆时针,所以用判断是否在多边形内的模板,不用是否在凸多边形内的模板 POJ 1584 A Round Peg in a G ...
- A Round Peg in a Ground Hole(凸包应用POJ 1584)
A Round Peg in a Ground Hole Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5684 Accepte ...
- 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 判断凸多边形,判断点在凸多边形内
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
A Round Peg in a Ground Hole Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5741 Acc ...
- POJ - 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)
http://poj.org/problem?id=1584 题意 按照顺时针或逆时针方向输入一个n边形的顶点坐标集,先判断这个n边形是否为凸包. 再给定一个圆形(圆心坐标和半径),判断这个圆是否完全 ...
随机推荐
- 动效解析工厂:Mask 动画
转载自:http://www.cocoachina.com/ios/20160214/15250.html 前言:很多动效都是多种动画的组合,有时候你可能只是需要其中某个动画,但面对庞杂的代码库或是教 ...
- 导出文本、表格、图像到PDF格式文件中(学习整理)
1.测试例子: 需要导入的外部jar包: 相关API http://www.coderanch.com/how-to/javadoc/itext-2.1.7/com/lowagie/text/pack ...
- dp题目
从别的地方看来,最近一直在啃DP,有个目标,更有动力了. 1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955 背包; ...
- 如何给html元素的onclick事件传递参数(即如何获取html标签的data-*属性)
现在做的一个小系统为了达到领导所说的很炫的效果有用到Metro UI CSS,但是因为如何给每个磁贴(div标签)的click事件传递参数折腾了蛮久(偶是菜鸟),后来终于找到一个解决方案即通过data ...
- php邮箱找回密码功能
原理很简单: 用户找回密码的时候,填写用户名,程序得到用户名便可以去数据库取出用户对应的密码以及当时填写的邮箱, 根据用户名和密码生成一个key=md5(username+password),然后$s ...
- General Purpose Hash Function Algorithms
General Purpose Hash Function Algorithms post@: http://www.partow.net/programming/hashfunctions/inde ...
- keil对51单片机变量和函数的编译处理
(1)初始值不是0的全局变量 在程序调到main()函数执行前,除了要进行内存清零.初始化堆栈外,还需要将全局变量的初始值加载到RAM的指定区域(编译过程中为全局变量分配的空间). (2)未初始化的局 ...
- QueryRunner的使用
在相继学习了JDBC和数据库操作之后,我们明显感到编写JDBC代码并非一件轻松的事儿.为了帮助我们更高效的学习工作,从JDBC的繁重代码中解脱出来,老佟给我们详尽介绍了一个简化JDBC操作的组件——D ...
- Netty启动分析
基于Netty-3.2.5 先看一段Netty的服务端代码: import java.net.InetSocketAddress; import java.util.concurrent.Execut ...
- MFC 之ActiveX控件学习
本文将介绍ActiveX控件的应用与工作原理,读者可以把ActiveX控件看成一个极小服务器的应用程序,它不能独立运行,必须要嵌入到容器程序中与容器一起运行,就像电脑主机中的显卡,它自己在电脑硬件系统 ...