A Round Peg in a Ground Hole
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5456   Accepted: 1735

Description

The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces of wood are attached to one another using a wooden peg that fits into pre-cut holes in each piece to be attached. The pegs have a circular cross-section and so are intended to fit inside a round hole. 
A recent factory run of computer desks were flawed when an automatic grinding machine was mis-programmed. The result is an irregularly shaped hole in one piece that, instead of the expected circular shape, is actually an irregular polygon. You need to figure out whether the desks need to be scrapped or if they can be salvaged by filling a part of the hole with a mixture of wood shavings and glue. 
There are two concerns. First, if the hole contains any protrusions (i.e., if there exist any two interior points in the hole that, if connected by a line segment, that segment would cross one or more edges of the hole), then the filled-in-hole would not be structurally sound enough to support the peg under normal stress as the furniture is used. Second, assuming the hole is appropriately shaped, it must be big enough to allow insertion of the peg. Since the hole in this piece of wood must match up with a corresponding hole in other pieces, the precise location where the peg must fit is known. 
Write a program to accept descriptions of pegs and polygonal holes and determine if the hole is ill-formed and, if not, whether the peg will fit at the desired location. Each hole is described as a polygon with vertices (x1, y1), (x2, y2), . . . , (xn, yn). The edges of the polygon are (xi, yi) to (xi+1, yi+1) for i = 1 . . . n − 1 and (xn, yn) to (x1, y1).

Input

Input consists of a series of piece descriptions. Each piece description consists of the following data: 
Line 1 < nVertices > < pegRadius > < pegX > < pegY > 
number of vertices in polygon, n (integer) 
radius of peg (real) 
X and Y position of peg (real) 
n Lines < vertexX > < vertexY > 
On a line for each vertex, listed in order, the X and Y position of vertex The end of input is indicated by a number of polygon vertices less than 3.

Output

For each piece description, print a single line containing the string: 
HOLE IS ILL-FORMED if the hole contains protrusions 
PEG WILL FIT if the hole contains no protrusions and the peg fits in the hole at the indicated position 
PEG WILL NOT FIT if the hole contains no protrusions but the peg will not fit in the hole at the indicated position

Sample Input

5 1.5 1.5 2.0
1.0 1.0
2.0 2.0
1.75 2.0
1.0 3.0
0.0 2.0
5 1.5 1.5 2.0
1.0 1.0
2.0 2.0
1.75 2.5
1.0 3.0
0.0 2.0
1

Sample Output

HOLE IS ILL-FORMED
PEG WILL NOT FIT

Source

这道题其实并不难,难得是独栋题意,这个题比较难懂,可能是英语比较差吧,读了好久。。。
题目大意: 给定n个点,但是不确定是顺时针还是逆时针的,判断它是否是凸多边形,如果是凸度变形的话,题目给定了一个圆心和半径,让求圆是否在凸多边形内。
关键步骤:
  1. 判断是否为凸多边形
  2. 如果是凸多边形,判断圆心在不在多边形内(边上也算)
  3. 如果圆心在,判断整个圆在不在,这时候就比较圆心到各个边的距离与半径的关系就行了
下面是用叉积来判断是否是凸多边形,用向量的点乘积来判断圆是否在多边形内,代码如下:
/*************************************************************************
> File Name: poj_1584_back.cpp
> Author: Howe_Young
> Mail: 1013410795@qq.com
> Created Time: 2015年04月06日 星期一 19时03分03秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#define EPS 1e-8
using namespace std;
struct point{
double x, y;
};
const int N = ;
point p[N];
double Min(double a, double b)
{
return a < b ? a : b;
}
double Max(double a, double b)
{
return a > b ? a : b;
}
int sgn(double d)
{
if (fabs(d) < EPS)
return ;
return d > ? : -;
}
double x_mutli(point p1, point p2)
{
return (p1.x * p2.y - p2.x * p1.y);
}
double dot_multi(point p1, point p2)
{
return (p1.x * p2.x + p1.y * p2.y);
}
//方向
double get_direction(point p1, point p2, point p3)
{
point v1, v2;
v1.x = p3.x - p1.x; v1.y = p3.y - p1.y;
v2.x = p2.x - p1.x; v2.y = p2.y - p1.y;
return x_mutli(v1, v2);
}
//得到模长
double get_length_of_mold(point p)
{
return sqrt(p.x * p.x + p.y * p.y);
}
bool is_vonvex(int n)
{
double tmp1 = 0.0, tmp2;
for (int i = ; i < n; i++)//因为凸多边形的相邻边的拐向都相同,要么都顺时针,要么多逆时针
{
tmp2 = sgn(get_direction(p[i], p[(i+)%n], p[(i+)%n]));
if (tmp1 * tmp2 < -EPS)
return false;
tmp1 = tmp2;
}
return true;
} //叉积判断点是否在多边形内,只适合凸多边形
bool point_in_polygon(point peg, int n)
{
double tmp1 = 0.0, tmp2;
for (int i = ; i < n; i++)
{
tmp2 = sgn(get_direction(p[i], p[(i+)%n], peg));
if (tmp1 * tmp2 < -EPS)
return false;
tmp1 = tmp2;
}
return true;
}
//判断圆是否在多边形内,就是判断点到边的最小离跟半径的关系
bool circle_in_polygon(point peg, double peg_r, int n)
{
if (peg_r == 0.0)
return true;
double shadow;//v1向量在v2向量上的投影长度 a点乘b然后除以b的模就是a在b上的投影
point v1, v2;
double ans;
for (int i = ; i < n; i++)
{
v1.x = peg.x - p[i].x; v1.y = peg.y - p[i].y;
v2.x = p[(i+)%n].x - p[i].x; v2.y = p[(i+)%n].y - p[i].y;
shadow = dot_multi(v1, v2) / (v2.x * v2.x + v2.y * v2.y) * 1.0;//这里是求投影占v2向量模的长度 的比例,如果大于1,或者小于0, 垂足肯定在外面了
if (shadow >= 0.0 && shadow <= 1.0)//利用面积来求高,也就是距离,叉乘的绝对值是三角形面积的两倍
ans = fabs(x_mutli(v1, v2)) / get_length_of_mold(v2);
else
{
//如果垂足在外面,找最近的一个端点
v2.x = peg.x - p[(i+)%n].x; v2.y = peg.y - p[(i+)%n].y;
ans = sgn(get_length_of_mold(v1) - get_length_of_mold(v2)) == - ? get_length_of_mold(v1) : get_length_of_mold(v2);
}
if (ans - peg_r < -EPS)//如果相交,返回false
return false;
}
return true; }
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n;
point peg;
double peg_r;
while (~scanf("%d", &n) && n >= )
{
scanf("%lf %lf %lf", &peg_r, &peg.x, &peg.y);
for (int i = ; i < n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);
if (!is_vonvex(n))//判断是否是凸多边形
{
puts("HOLE IS ILL-FORMED");
continue;
}
if (point_in_polygon(peg , n) && circle_in_polygon(peg, peg_r, n))
{
puts("PEG WILL FIT");
}
else
puts("PEG WILL NOT FIT");
}
return ;
}

用射线的方法来判断点是否在多边形内

bool on_line(point p1, point p2, point p3)
{
if (p3.x >= Min(p1.x, p2.x) && p3.x <= Max(p1.x, p2.x) && p3.y <= Min(p1.y, p2.y) && p3.y <= Max(p1.y, p2.y))
return sgn(get_direction(p1, p2, p3)) == ;
return false;
}
//射线的方法判别点在多边形内
bool point_in_polygon_ray(point peg, int n, double peg_r)
{
int counter = ;
double xinter;
point p1, p2;
p1 = p[];
for (int i = ; i <= n; i++)
{
p2 = p[i % n];
if (on_line(p1, p2, peg))
if (sgn(peg_r) == )
return false;
else
return true;
if (peg.y > Min(p1.y, p2.y))
{
if (peg.y <= Max(p1.y, p2.y))
{
if (peg.x <= Max(p1.x, p2.x))
{
if (p1.y != p2.y)
{
xinter = (peg.y - p2.y) * (p1.x - p2.x) / (p1.y - p2.y) + p2.x;
if (p1.x == p2.x || peg.x <= xinter)
counter++;
}
}
}
}
p1 = p2;
}
if (counter % == )
return false;
return true;
}

角度和判定是否在多边形内

//判断点在直线上  p3在p1p2上
bool point_on_line(point p1, point p2, point p3)
{
if (p3.x >= Min(p1.x, p2.x) && p3.x <= Max(p1.x, p2.x) && p3.y >= Min(p1.y, p2.y) && p3.y <= Max(p1.y, p2.y))
return sgn(get_direction(p1, p2, p3)) == ;
return false;
}
//判断点是否在多边形内,角度和算法
bool point_is_inside_angle(point peg, int n)
{
double sum = 0.0;
point v1, v2, v3;
for (int i = ; i < n; i++)
{
if (peg == p[i])
return true;
if (p[i] == p[(i + ) % n])
continue;
if (point_on_line(p[i], p[(i+)%n], peg))
return true;
v1.x = peg.x - p[i].x; v1.y = peg.y - p[i].y;
v2.x = peg.x - p[(i+)%n].x; v2.y = peg.y - p[(i+)%n].y;
v3.x = p[i].x - p[(i+)%n].x; v3.y = p[i].y - p[(i+)%n].y;
double a = get_length_of_mold(v1);
double b = get_length_of_mold(v2);
double c = get_length_of_mold(v3);
sum += sgn(x_mutli(v1, v2)) * acos((a * a + b * b - c * c) / (2.0 * a * b));
}
sum = fabs(sum);
if (sgn(sum - 2.0 * PI) == )
return true;
return false;
}

改进弧长法(这个方法还未理解,如果哪位大神路过这,麻烦留一下言)

double x_multi_2(point p1,point p2,point p3)
{
return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);
}
int get_tmp(point p0)
{
return p0.x>=0?(p0.y>=0?0:3):(p0.y>=0?1:2);
}
//改进弧长法
bool point_is_inside_arc(point peg, int n)
{
int tmp1,tmp2,sum=0,i;
point p0,p1;
p0.x=peg.x,p0.y=peg.y;
p1.x=p[0].x-p0.x,p1.y=p[0].y-p0.y;
tmp1=get_tmp(p1); for(i=0;i<n;i++)
{
if(p[i]==p0)
break;
int t0=sgn(x_multi_2(p[i],p[(i+1)%n], p0));
int t1=sgn((p[i].x-p0.x)*(p[(i+1)%n].x-p0.x));
int t2=sgn((p[i].y-p0.y)*(p[(i+1)%n].y-p0.y)); if(!t0&&t1<=0&&t2<=0) //被测点在多边形边上
break;
p1.x=p[(i+1)%n].x-p0.x,p1.y=p[(i+1)%n].y-p0.y;
tmp2=get_tmp(p1); //计算象限
switch((tmp2-tmp1+4)%4)
{
case 1:{ sum++; break; }
case 2:
{
if(t0>0) sum+=2;
else sum-=2;
break;
}
case 3: { sum--; break; }
}
tmp1=tmp2;
}
if(i<n||sum) //被测点在多边形边上或者在多边形内部
return true;
return false;
}

  

 

POJ 1584 A Round Peg in a Ground Hole 判断凸多边形,判断点在凸多边形内的更多相关文章

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

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

  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(判断凸多边形,点到线段距离,点在多边形内)

    A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4438   Acc ...

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

  5. POJ 1584 A Round Peg in a Ground Hole

    先判断是不是N多边形,求一下凸包,如果所有点都用上了,那么就是凸多边形 判断圆是否在多边形内, 先排除圆心在多边形外的情况 剩下的情况可以利用圆心到每条边的最短距离与半径的大小来判断 #include ...

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

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

  7. POJ 1584 A Round Peg in a Ground Hole --判定点在形内形外形上

    题意: 给一个圆和一个多边形,多边形点可能按顺时针给出,也可能按逆时针给出,先判断多边形是否为凸包,再判断圆是否在凸包内. 解法: 先判是否为凸包,沿着i=0~n,先得出初始方向dir,dir=1为逆 ...

  8. 简单几何(点的位置) POJ 1584 A Round Peg in a Ground Hole

    题目传送门 题意:判断给定的多边形是否为凸的,peg(pig?)是否在多边形内,且以其为圆心的圆不超出多边形(擦着边也不行). 分析:判断凸多边形就用凸包,看看点集的个数是否为n.在多边形内用叉积方向 ...

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

随机推荐

  1. java学习之 垃圾回收

    垃圾回收器始终以一个较低优先级的后台进程进行垃圾的回收工作,这样不会影响程序的正常工作. 通常只有当内存到达用尽的边缘而程序需要分配新的内存空间时,垃圾回收器才会执行. 垃圾回收的条件:1,垃圾回收器 ...

  2. js的引用顺序

    注意:Bootstrap中的JS插件依赖于JQuery,因此JQuery要在Bootstrap之前引用!!! 把JS文件引用放入body的最下面,是为了使js在网页全部加载完后才起作用,比如你的js里 ...

  3. 2661: [BeiJing wc2012]连连看

    Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给出一个闭区间[a,b]中的全部整数,如果其中某两个数x,y( ...

  4. win7如何开启和关闭超级管理员账户

    激活命令: net user administrator /active:yes 关闭命令: net user administrator /active:no

  5. 用Jquery 仿VS 样式的 导航栏插件

    在开发B/S 项目过程中,根据主界面设计要求,需要做一个类似VS 左边工具栏样式的菜单导航栏,在网上搜索无果后,于是决定自已做一个. 由于前台用JQuery开发, 想到网上很多人用JQuery做插件, ...

  6. hadoop hdfs 命令行 设置文件夹大小的上限 quota:配额

    >bin/hdfs dfs -put readme.txt /finance >bin/hdfs dfs -du -s /finance > /finance >bin/hdf ...

  7. 【Java】Web 服务编程技巧与窍门: 在 UDDI 注册中心为 Web 服务注册开发 UDDI Java 应用程序

    本技巧建立了一个使用统一描述.发现和集成 (Universal Description, Discovery, and Integration,UDDI) 来注册应用程序级消费的 Web 服务实例.作 ...

  8. C#技术分享【PDF转换成图片——13种方案】(2013-07-25重新整理)

    原文:C#技术分享[PDF转换成图片--13种方案](2013-07-25重新整理) 重要说明:本博已迁移到 石佳劼的博客,有疑问请到 文章新地址 留言!!! 写在最前面:为了节约大家时间,撸主把最常 ...

  9. 【HDOJ】5179 beautiful number

    DFS. /* 5179 */ #include <iostream> #include <algorithm> #include <map> #include & ...

  10. Unity 通过Animation实现控件位置的转换

    Unity版本:4.5.1 NGUI版本:3.6.5 参考链接:http://blog.csdn.net/unity3d_xyz/article/details/23035521,作者:CSDN in ...