Points Within


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Statement of the Problem

Several drawing applications allow us to draw polygons and almost all of them allow us to fill them with some color. The task of filling a polygon reduces to knowing which points are inside it, so programmers have to colour only those points.

You're expected to write a program which tells us if a given point lies inside a given polygon described by the coordinates of its vertices. You can assume that if a point is in the border of the polygon, then it is in fact inside the polygon.

Input Format

The input file may contain several instances of the problem. Each instance consists of: (i) one line containing integers N, 0 < N < 100 and M, respectively the number of vertices of the polygon and the number of points to be tested. (ii) N lines, each containing a pair of integers describing the coordinates of the polygon's vertices; (iii) M lines, each containing a pair of integer coordinates of the points which will be tested for "withinness" in the polygon.

You may assume that: the vertices are all distinct; consecutive vertices in the input are adjacent in the polygon; the last vertex is adjacent to the first one; and the resulting polygon is simple, that is, every vertex is incident with exactly two edges and two edges only intersect at their common endpoint. The last instance is followed by a line with a 0 (zero).

Output Format

For the ith instance in the input, you have to write one line in the output with the phrase "Problem i:", followed by several lines, one for each point tested, in the order they appear in the input. Each of these lines should read "Within" or "Outside", depending on the outcome of the test. The output of two consecutive instances should be separated by a blank line.

Sample Input

3 1
0 0
0 5
5 0
10 2
3 2
4 4
3 1
1 2
1 3
2 2
0

Sample Output

Problem 1:
Outside

Problem 2:
Outside
Within


  

  计算几何,判断点是否在多边形内

  套用了以前写的模板,很easy的就过了。经典题。

  代码:

 #include <stdio.h>
struct Point{
double x,y;
};
struct Line{
Point p1,p2;
};
double xmulti(Point p1,Point p2,Point p0) //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double Max(double a,double b)
{
return a>b?a:b;
}
double Min(double a,double b)
{
return a<b?a:b;
}
bool ponls(Point q,Line l) //判断点q是否在线段l上
{
if(q.x > Max(l.p1.x,l.p2.x) || q.x < Min(l.p1.x,l.p2.x)
|| q.y > Max(l.p1.y,l.p2.y) || q.y < Min(l.p1.y,l.p2.y) )
return false;
if(xmulti(l.p1,l.p2,q)==) //点q不在l的延长线或者反向延长线上,如果叉积再为0,则确定点q在线段l上
return true;
else
return false;
}
bool pinplg(int pointnum,Point p[],Point q)
{
Line s;
int c = ;
for(int i=;i<=pointnum;i++){ //多边形的每条边s
if(i==pointnum)
s.p1 = p[pointnum],s.p2 = p[];
else
s.p1 = p[i],s.p2 = p[i+];
if(ponls(q,s)) //点q在边s上
return true;
if(s.p1.y != s.p2.y){ //s不是水平的
Point t;
t.x = q.x - ,t.y = q.y;
if( (s.p1.y == q.y && s.p1.x <=q.x) || (s.p2.y == q.y && s.p2.x <= q.x) ){ //s的一个端点在L上
int tt;
if(s.p1.y == q.y)
tt = ;
else if(s.p2.y == q.y)
tt = ;
int maxx;
if(s.p1.y > s.p2.y)
maxx = ;
else
maxx = ;
if(tt == maxx) //如果这个端点的纵坐标较大的那个端点
c++;
}
else if(xmulti(s.p1,t,q)*xmulti(s.p2,t,q) <= ){ //L和边s相交
Point lowp,higp;
if(s.p1.y > s.p2.y)
lowp.x = s.p2.x,lowp.y = s.p2.y,higp.x = s.p1.x,higp.y = s.p1.y;
else
lowp.x = s.p1.x,lowp.y = s.p1.y,higp.x = s.p2.x,higp.y = s.p2.y;
if(xmulti(q,higp,lowp)>=)
c++;
}
}
}
if(c%==)
return false;
else
return true;
}
int main()
{
int i,n,m,num=;
while(scanf("%d",&n)!=EOF){
if(n==) break;
scanf("%d",&m);
Point p[];
for(i=;i<=n;i++) //输入多边形的顶点
scanf("%lf%lf",&p[i].x,&p[i].y);
if(num!=) //如果不是第一组数据块就先输出一个空行
printf("\n");
printf("Problem %d:\n",num++);
for(i=;i<=m;i++){
Point t;
scanf("%lf%lf",&t.x,&t.y);
if(pinplg(n,p,t))
printf("Within\n");
else
printf("Outside\n");
}
}
return ;
}

Freecode : www.cnblogs.com/yym2013

zoj 1081:Points Within(计算几何,判断点是否在多边形内,经典题)的更多相关文章

  1. zoj 1081 Points Within (判断点是否在多边形内)

    http://blog.csdn.net/zxy_snow/article/details/6339621先保存,搞懂了再来写

  2. ZOJ 1081 Points Within( 判断点在多边形内外 )

    链接:传送门 题意:给出n个点围成的一个多边形,现在有m个点p,询问p是否在多边形内,你可以认为这些点均不同且输入的顶点是多边形中相邻的两个顶点,最后的顶点与第一个相邻并且每一个顶点都连接两条边( 左 ...

  3. HDU - 4458 计算几何判断点是否在多边形内

    思路:将飞机看成不动的,然后枚举时间看点是否在多边形内部. #include<bits/stdc++.h> #define LL long long #define fi first #d ...

  4. [zoj] 1081 Points Within || 判断点是否在多边形内

    原题 多组数据. n为多边形顶点数,m为要判断的点数 按逆时针序给出多边形的点,判断点是否在多边形内,在的话输出"Within",否则输出"Outside" / ...

  5. hrbustoj 1429:凸多边形(计算几何,判断点是否在多边形内,二分法)

    凸多边形 Time Limit: 2000 MS    Memory Limit: 65536 K Total Submit: 130(24 users)   Total Accepted: 40(1 ...

  6. hrbustoj 1306:再遇攻击(计算几何,判断点是否在多边形内,水题)

    再遇攻击 Time Limit: 1000 MS    Memory Limit: 65536 K Total Submit: 253(37 users)   Total Accepted: 56(2 ...

  7. 百度地图 判断marker是否在多边形内

    昨天画了圆形,判marker是否存在圆形内.今天来画多边形,判断marker在多边形内. 需要引入一个js      <script type="text/javascript&quo ...

  8. C# 判断点是否在多边形内

    /// <summary>/// 判断点是否在多边形内/// </summary>/// <param name="pnt">点</par ...

  9. ZOJ 1081 Points Within | 判断点在多边形内

    题目: 给个n个点的多边形,n个点按顺序给出,给个点m,判断m在不在多边形内部 题解: 网上有两种方法,这里写一种:射线法 大体的思想是:以这个点为端点,做一条平行与x轴的射线(代码中射线指向x轴正方 ...

随机推荐

  1. 在Ubuntu 12 服务器上源码安装 OpenERP 8.0

    原文:http://vivianyw.blog.163.com/blog/static/134547422201421112349489/ 1. 安装SSH: sudo apt-get install ...

  2. chrome 此网页正试图从未经验证的来源加载脚本

      chrome 此网页正试图从未经验证的来源加载脚本 CreateTime--2018年5月25日08点02分 Author:Marydon 1.情景还原 以我的网站为例,https://www.c ...

  3. HTML5 学习笔记 表单属性

    HTML5新的表单属性 HTML5 的form和input 标签添加了几个新的属性 <form>新属性 autocomplete novalidate input 新属性 autocomp ...

  4. ajax表单提交较慢原因的解决办法

    ajax提交表单时,发现过了好长时间才有反应.使用F12打开开发人员工具一看,发现提示“provisional headers are shown”. 百度了一下,才知道可能是ajax异步提交和for ...

  5. RPC服务框架dubbo(六):Consumer搭建过程

    1.在pom.xml中除了ssm的依赖添加dubbo相关3个依赖(接口,dubbo.jar,zkClient) 2.web.xml中修改<init-value>applicationCon ...

  6. 第二节 JVM优化应用以及知识总结

    在JVM中.假设98%的时间是用于GC且可用的HeapSize不足2%时将会抛出OOM异常:HeapSize最大不要超过可用物理内存的80%,一般-Xms –Xmx设置为同样,-Xmn设置为1/4的- ...

  7. Atitit.web预览播放视频的总结

    Atitit.web预览播放视频的总结 1. 浏览器类型的兼容性(chrome,ff,ie) 1 2. 操作系统的兼容性 1 3. 视频格式的内部视频格式跟播放器插件的兼容性.. 2 4. 指定播放器 ...

  8. http_load使用详解

    1.什么是http_loadhttp_load是一款基于Linux平台的web服务器性能测试工具,用于测试web服务器的吞吐量与负载,web页面的性能. 2.http_load的安装1)下载地址wge ...

  9. mysql 创建连接是 Cannot create PoolableConnectionFactory (Unknown character set: 'utf8mb4')

    Cannot create PoolableConnectionFactory (Unknown character set: 'utf8mb4') maven 依赖换版本 <dependenc ...

  10. 删除节点removeChild()

    http://www.imooc.com/code/1700 删除节点removeChild() removeChild() 方法从子节点列表中删除某个节点.如删除成功,此方法可返回被删除的节点,如失 ...