题目大意:首先给一个圆的半径和圆心,然后给一个多边形的所有点(多边形按照顺时针或者逆时针给的),求,这个多边形是否是凸多边形,如果是凸多边形在判断这个圆是否在这个凸多边形内。
 
分析:判断凸多边形可以使用相邻的三个点叉积判断,因为不知道顺时针还是逆时针,所以叉积如果有有整数和负数,那么一定不是凸多边形(注意允许多多点在一条线段上)。判断圆在凸多边形首先要判断圆心是否在多边形内,如果在多边形内,再次判断圆心到达到变形每条边的最短距离,如果小于半径就是不合法。ps:一道好题,通过这个题学会了不少东西。
 
代码如下:
=======================================================================================================================================
#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 (判断凸多边形&判断点在多边形内&判断圆在多边形内)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. POJ 1584 A Round Peg in a Ground Hole 判断凸多边形 点到线段距离 点在多边形内

    首先判断是不是凸多边形 然后判断圆是否在凸多边形内 不知道给出的点是顺时针还是逆时针,所以用判断是否在多边形内的模板,不用是否在凸多边形内的模板 POJ 1584 A Round Peg in a G ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

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

    http://poj.org/problem?id=1584 题意 按照顺时针或逆时针方向输入一个n边形的顶点坐标集,先判断这个n边形是否为凸包. 再给定一个圆形(圆心坐标和半径),判断这个圆是否完全 ...

随机推荐

  1. html5 + css3 + zepto.js实现的微信广告宣传页

    最新学习html5 + css3, 参考微信的一个推广页写出一个实例巩固自己知识,自己已经将原实例打包到自己博客文件当中,但是不知道如何提供下载,如有需要的朋友可以联系我qq309666726

  2. 2D动态光照

    对场景内所有点发出射线, 如果射线被某条边阻挡, 则射线停留在阻挡的边上, 如果射线顺利抵达终点, 则对射线偏移-0.001, +0.001角度, 再射出2条射线, 停留在后续的阻挡边上. 把最终的射 ...

  3. [LeetCode OJ] Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  4. python隐含的特性

    本文源自(http://stackoverflow.com/questions/101268/hidden-features-of-python)希望介绍Python非常有用,而比较忽视的Python ...

  5. Android 6.0 Permission权限与安全机制

    Marshmallow版本权限修改 android的权限系统一直是首要的安全概念,因为这些权限只在安装的时候被询问一次.一旦安装了,app可以在用户毫不知晓的情况下访问权限内的所有东西,而且一般用户安 ...

  6. Python 学习之urllib模块---用于发送网络请求,获取数据

    1.urllib urllib是Python标准库的一部分,包含urllib.request,urllib.error,urllib.parse,urlli.robotparser四个子模块. (1) ...

  7. User Commands

    archive Creates a hadoop archive[v.存档; n.档案文件; 档案室; ]. More information can be found at Hadoop Archi ...

  8. gocode 安装

    1.安装git,将git/bin添加至PATH 2.执行go get -u github.com/nsf/gocode 3.在%GOPATH%/bin/下会生成gocode.exe

  9. 2016021801 - Java内存区域归纳对比

    线程私有 线程共享 程序计数器,虚拟机栈,本地方法栈 堆,方法区 内存区 异常 异常原因 程序计数器 无 虚拟机栈 StackOverflowError 线程请求的栈深度大于虚拟机栈所允许的深度 Ou ...

  10. net Core 通过 Ef Core 访问、管理Mysql

    net Core 通过 Ef Core 访问.管理Mysql 本文地址:http://www.cnblogs.com/likeli/p/5910524.html 环境 dotnet Core版本:1. ...