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. 在MVC的cshtml视图页获取默认路由下的ID值的方法

    <a href="/user/resume/index/11"> <span class="title bold">我的 @Reques ...

  2. HMM隐Markov模型的原理及应用建模

    这里不讲定量的公式.(由于我也没全然弄明确.不想误人子弟)仅仅谈高速定性理解. 隐Markov模型原理 隐Markov模型(Hidden Markov Model.HMM)的实质就是:已知几种原始分类 ...

  3. AP*更新供应商地点

    --更新供应商地点 PROCEDURE update_vendor_site(p_init_msg_list IN VARCHAR2 DEFAULT fnd_api.g_false, x_return ...

  4. 演示unity内存管理机制的缺陷

    概述 这是最近做项目时发现的一个内存管理机制上的一个缺陷,但是我并不知道这究竟是不是一个bug,因为他可以造成内存泄漏,但是却能避开野指针. 详细 代码下载:http://www.demodashi. ...

  5. AI:人工智能搜索策略

    人工智能搜索策略:

  6. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  7. 创建你的第一个Android PHP应用

    google的开源移动操作系统Android给智能手机市场带来了风暴.不像Apple,对想要为水果市场(Iphone App Store)提供应用软件的开发者们有着严格的指导原则以及要求,Google ...

  8. MySQL修改root密码的各种方法整理(转)

    整理了以下四种在MySQL中修改root密码的方法,可能对大家有所帮助! 方法1: 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR ' ...

  9. Uva 10815-Andy&#39;s First Dictionary(串)

    Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce ...

  10. opensips编译安装时可能遇到的问题

    错误一: ERROR: could not load the script in /usr/local//lib64/opensips/opensipsctl/opensipsdbctl.pgsql ...