题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=81Points 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


Source: South America 2001

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

题意就是判断是否在多边形内,是个的话就输出Within……

看书上敲的,不是太懂,没用到射线求交点稀里糊涂的就求出来了

先贴个好点的代码:转至:http://blog.csdn.net/zxy_snow/article/details/6339621

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAX = ;
const double eps = 1e-;
struct point
{
double x,y;
};
struct beeline
{
point a,b;
};
point p[MAX];
int n; bool dy(double x,double y) // x > y
{
return x > y + eps;
}
bool xy(double x,double y) // x < y
{
return x < y - eps;
}
bool dyd(double x,double y) // x >= y
{
return x > y - eps;
}
bool xyd(double x,double y) // x <= y
{
return x < y + eps;
}
bool dd(double x,double y) // x == y
{
return fabs( x - y ) < eps;
}
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向
{
return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
bool onSegment(point a, point b, point c)
{
double maxx = max(a.x,b.x);
double maxy = max(a.y,b.y);
double minx = min(a.x,b.x);
double miny = min(a.y,b.y);
if( dd(crossProduct(a,b,c),0.0) && dyd(c.x,minx) && xyd(c.x,maxx) && dyd(c.y,miny) && xyd(c.y,maxy) )
return true;
return false;
}
bool segIntersect(point p1,point p2, point p3, point p4)
{
double d1 = crossProduct(p3,p4,p1);
double d2 = crossProduct(p3,p4,p2);
double d3 = crossProduct(p1,p2,p3);
double d4 = crossProduct(p1,p2,p4);
if( xy(d1 * d2,0.0) && xy( d3*d4,0.0 ) )
return true;
if( dd(d1,0.0) && onSegment(p3,p4,p1) )
return true;
if( dd(d2,0.0) && onSegment(p3,p4,p2) )
return true;
if( dd(d3,0.0) && onSegment(p1,p2,p3) )
return true;
if( dd(d4,0.0) && onSegment(p1,p2,p4) )
return true;
return false;
}
bool inPolygon(point pot)
{
int count = ;
beeline l;
l.a = pot;
l.b.x = 1e10;
l.b.y = pot.y;
p[n] = p[];
for(int i=; i<n; i++)
{
if( onSegment(p[i],p[i+],pot) )
return true;
if( !dd(p[i].y,p[i+].y) )//水平边不考虑
{
int tmp = -;
if( onSegment(l.a,l.b,p[i]) )//对于顶点与射线相交,该顶点应是所属边上纵坐标上较大的
tmp = i;
else if( onSegment(l.a,l.b,p[i+]) )
tmp = i+;
if( tmp != - && dd(p[tmp].y,max(p[i].y,p[i+].y)) )
count++;
else if( tmp == - && segIntersect(p[i],p[i+],l.a,l.b) )//相交
count++;
}
}
if( count % == )
return true;
return false;
}
int main()
{
int m;
int ind = ;
point pot;
while( ~scanf("%d",&n) && n )
{
if( ind != )
printf("\n");
scanf("%d",&m);
for(int i=; i<n; i++)
scanf("%lf %lf",&p[i].x,&p[i].y);
printf("Problem %d:\n",ind++);
while( m-- )
{
scanf("%lf %lf",&pot.x,&pot.y);
if( inPolygon(pot) )
printf("Within\n");
else
printf("Outside\n");
}
}
return ;
}

下面是我的代码,写的自己不是很懂

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <map> #define eps 1.0e-5
inline double max(double a,double b){return a>b?a:b;}
inline double min(double a,double b){return a<b?a:b;}
inline double dabs(double a ){return a<?-a:a;} struct point
{
double x,y;
}; point poly[];
int n,m; bool online(const point &p1,const point &p2,const point &p3)
{
if(p2.x>=min(p1.x,p3.x)&&p2.x<=max(p1.x,p3.x)&&
p2.y>=min(p1.y,p3.y)&&p2.y<=max(p1.y,p3.y))
{
if(dabs((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))<=eps)
return true;
}
return false;
} bool insidepolygon(point p)
{
int count = ;
double xinters;
point p1,p2;
p1=poly[]; for(int i=;i<=n;i++)
{
p2=poly[i%n];
if(online(p1,p,p2))return true;
if(p.y>min(p1.y,p2.y))
{
if(p.y<=max(p1.y,p2.y))
{
if(p.x<=max(p1.x,p2.x))
{
if(p1.y!=p2.y)
{
xinters=(p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
//(p.y-p1.y)/(xinter-p1.x)=(p2.y-p1.y)/(p2.x-p1.x)斜率
if(p1.x == p2.x||p.x<=xinters)count++;
}
}
}
}
p1=p2;
}
if(count% == )
return false;
return true;
} int main()
{
int i,j;
point p;
int cas=;
while(scanf("%d",&n)!=EOF)
{
if(n == )break;
if(cas>)printf("\n");
printf("Problem %d:\n",cas++); scanf("%d",&m);
for(i=;i<n;i++)
{
scanf("%lf%lf",&poly[i].x,&poly[i].y);
} for(j=;j<m;j++)
{
scanf("%lf%lf",&p.x,&p.y);
if(insidepolygon(p))
{
printf("Within\n");
}
else
{
printf("Outside\n");
}
}
}
return ;
}

zoj 1081 判断点在多边形内的更多相关文章

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

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

  2. 判断点在多边形内算法的C++实现

    目录 1. 算法思路 2. 具体实现 3. 改进空间 1. 算法思路 判断平面内点是否在多边形内有多种算法,其中射线法是其中比较好理解的一种,而且能够支持凹多边形的情况.该算法的思路很简单,就是从目标 ...

  3. hdu 1756:Cupid's Arrow(计算几何,判断点在多边形内)

    Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. php之判断点在多边形内的api

    1.判断点在多边形内的数学思想:以那个点为顶点,作任意单向射线,如果它与多边形交点个数为奇数个,那么那个点在多边形内,相关公式: <?php class AreaApi{ //$area是一个多 ...

  5. POJ 2318 TOYS | 二分+判断点在多边形内

    题意: 给一个矩形的区域(左上角为(x1,y1) 右下角为(x2,y2)),给出n对(u,v)表示(u,y1) 和 (v,y2)构成线段将矩形切割 这样构成了n+1个多边形,再给出m个点,问每个多边形 ...

  6. R树判断点在多边形内-Java版本

    1.什么是RTree 待补充 2.RTree java依赖 rtree的java开源版本在GitHub上:https://github.com/davidmoten/rtree 上面有详细的使用说明 ...

  7. hdu 1756 判断点在多边形内 *

    模板题 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> ...

  8. A Round Peg in a Ground Hole - POJ 1584 (判断凸多边形&判断点在多边形内&判断圆在多边形内)

    题目大意:首先给一个圆的半径和圆心,然后给一个多边形的所有点(多边形按照顺时针或者逆时针给的),求,这个多边形是否是凸多边形,如果是凸多边形在判断这个圆是否在这个凸多边形内.   分析:判断凸多边形可 ...

  9. matlab inpolygon 判断点在多边形内

    如何判断一个点在多边形内部? xv= [0 3 3 0 0]; %x坐标 yv= [0 0 3 3 0];%y坐标 x=1.5; y=1.5; in=inpolygon(x,y,xv,yv) plot ...

随机推荐

  1. javax.transaction.xa.XAException: java.sql.SQLException: 无法创建 XA 控制连接。(SQL 2000,SQL2005,SQL2008)

    javax.transaction.xa.XAException: java.sql.SQLException:无法创建 XA 控制连接.错误: 未能找到存储过程'master..xp_sqljdbc ...

  2. 鸟哥的linux私房菜学习记录之程序管理和SElinux初探

    process是进程的意思也就是说进程是正在运行的程序 将后台程序的错误信息等等输出到某个文档 终端关闭后会停止运行,如果想终端关闭后继续运行可以使用nohup命令,man nohup.

  3. Android NDK开发(五)--C代码回调Java代码【转】

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/41862479 在上篇博客里了解了Java层是怎样传递数据到C层代码,并且熟悉了大部 ...

  4. 161012、JAVA读写文件,如何避免中文乱码

    1.JAVA读取文件,避免中文乱码. /** * 读取文件内容 * * @param filePathAndName * String 如 c:\\1.txt 绝对路径 * @return boole ...

  5. 用grunt搭建自动化的web前端开发环境实战教程(详细步骤)

    用grunt搭建自动化的web前端开发环境实战教程(详细步骤) jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用!前端自动化, ...

  6. 转载:JMS-ActiveMQ浅析

    ActiveMQ 即时通讯服务 浅析 一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE ...

  7. Mysql String Functions

    SUBSTRING_INDEX(str,delim,count) 按标识符截取指定长度的字符串 mysql); -> 'www.mysql' mysql); -> 'mysql.com' ...

  8. Hibernate,JPA注解@SecondaryTable

    使用类一级的 @SecondaryTable或@SecondaryTables注解可以实现单个实体到多个表的映射. 使用 @Column或者 @JoinColumn注解中的table参数可指定某个列所 ...

  9. 使用PHP flush 函数的时候我们需要注意些什么呢?

    WebServer(可以认为特指apache)的缓冲区.在apache module的sapi下, flush会通过调用sapi_module的flush成员函数指针,间接的调用apache的api: ...

  10. Winform中如何实现父窗体传递数据到子窗体并刷新子窗体

    原理:利用委托和事件,本文将以图文并茂的例子讲述,告诉我们So Easy --------------------------------------------------------------- ...