题目链接: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

  1. #include <queue>
  2. #include <stack>
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <iostream>
  7. #include <limits.h>
  8. #include <string.h>
  9. #include <algorithm>
  10. using namespace std;
  11. const int MAX = ;
  12. const double eps = 1e-;
  13. struct point
  14. {
  15. double x,y;
  16. };
  17. struct beeline
  18. {
  19. point a,b;
  20. };
  21. point p[MAX];
  22. int n;
  23.  
  24. bool dy(double x,double y) // x > y
  25. {
  26. return x > y + eps;
  27. }
  28. bool xy(double x,double y) // x < y
  29. {
  30. return x < y - eps;
  31. }
  32. bool dyd(double x,double y) // x >= y
  33. {
  34. return x > y - eps;
  35. }
  36. bool xyd(double x,double y) // x <= y
  37. {
  38. return x < y + eps;
  39. }
  40. bool dd(double x,double y) // x == y
  41. {
  42. return fabs( x - y ) < eps;
  43. }
  44. double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向
  45. {
  46. return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
  47. }
  48. bool onSegment(point a, point b, point c)
  49. {
  50. double maxx = max(a.x,b.x);
  51. double maxy = max(a.y,b.y);
  52. double minx = min(a.x,b.x);
  53. double miny = min(a.y,b.y);
  54. if( dd(crossProduct(a,b,c),0.0) && dyd(c.x,minx) && xyd(c.x,maxx) && dyd(c.y,miny) && xyd(c.y,maxy) )
  55. return true;
  56. return false;
  57. }
  58. bool segIntersect(point p1,point p2, point p3, point p4)
  59. {
  60. double d1 = crossProduct(p3,p4,p1);
  61. double d2 = crossProduct(p3,p4,p2);
  62. double d3 = crossProduct(p1,p2,p3);
  63. double d4 = crossProduct(p1,p2,p4);
  64. if( xy(d1 * d2,0.0) && xy( d3*d4,0.0 ) )
  65. return true;
  66. if( dd(d1,0.0) && onSegment(p3,p4,p1) )
  67. return true;
  68. if( dd(d2,0.0) && onSegment(p3,p4,p2) )
  69. return true;
  70. if( dd(d3,0.0) && onSegment(p1,p2,p3) )
  71. return true;
  72. if( dd(d4,0.0) && onSegment(p1,p2,p4) )
  73. return true;
  74. return false;
  75. }
  76. bool inPolygon(point pot)
  77. {
  78. int count = ;
  79. beeline l;
  80. l.a = pot;
  81. l.b.x = 1e10;
  82. l.b.y = pot.y;
  83. p[n] = p[];
  84. for(int i=; i<n; i++)
  85. {
  86. if( onSegment(p[i],p[i+],pot) )
  87. return true;
  88. if( !dd(p[i].y,p[i+].y) )//水平边不考虑
  89. {
  90. int tmp = -;
  91. if( onSegment(l.a,l.b,p[i]) )//对于顶点与射线相交,该顶点应是所属边上纵坐标上较大的
  92. tmp = i;
  93. else if( onSegment(l.a,l.b,p[i+]) )
  94. tmp = i+;
  95. if( tmp != - && dd(p[tmp].y,max(p[i].y,p[i+].y)) )
  96. count++;
  97. else if( tmp == - && segIntersect(p[i],p[i+],l.a,l.b) )//相交
  98. count++;
  99. }
  100. }
  101. if( count % == )
  102. return true;
  103. return false;
  104. }
  105. int main()
  106. {
  107. int m;
  108. int ind = ;
  109. point pot;
  110. while( ~scanf("%d",&n) && n )
  111. {
  112. if( ind != )
  113. printf("\n");
  114. scanf("%d",&m);
  115. for(int i=; i<n; i++)
  116. scanf("%lf %lf",&p[i].x,&p[i].y);
  117. printf("Problem %d:\n",ind++);
  118. while( m-- )
  119. {
  120. scanf("%lf %lf",&pot.x,&pot.y);
  121. if( inPolygon(pot) )
  122. printf("Within\n");
  123. else
  124. printf("Outside\n");
  125. }
  126. }
  127. return ;
  128. }

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

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <iostream>
  5. #include <stdlib.h>
  6. #include <map>
  7.  
  8. #define eps 1.0e-5
  9. inline double max(double a,double b){return a>b?a:b;}
  10. inline double min(double a,double b){return a<b?a:b;}
  11. inline double dabs(double a ){return a<?-a:a;}
  12.  
  13. struct point
  14. {
  15. double x,y;
  16. };
  17.  
  18. point poly[];
  19. int n,m;
  20.  
  21. bool online(const point &p1,const point &p2,const point &p3)
  22. {
  23. if(p2.x>=min(p1.x,p3.x)&&p2.x<=max(p1.x,p3.x)&&
  24. p2.y>=min(p1.y,p3.y)&&p2.y<=max(p1.y,p3.y))
  25. {
  26. if(dabs((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))<=eps)
  27. return true;
  28. }
  29. return false;
  30. }
  31.  
  32. bool insidepolygon(point p)
  33. {
  34. int count = ;
  35. double xinters;
  36. point p1,p2;
  37. p1=poly[];
  38.  
  39. for(int i=;i<=n;i++)
  40. {
  41. p2=poly[i%n];
  42. if(online(p1,p,p2))return true;
  43. if(p.y>min(p1.y,p2.y))
  44. {
  45. if(p.y<=max(p1.y,p2.y))
  46. {
  47. if(p.x<=max(p1.x,p2.x))
  48. {
  49. if(p1.y!=p2.y)
  50. {
  51. xinters=(p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  52. //(p.y-p1.y)/(xinter-p1.x)=(p2.y-p1.y)/(p2.x-p1.x)斜率
  53. if(p1.x == p2.x||p.x<=xinters)count++;
  54. }
  55. }
  56. }
  57. }
  58. p1=p2;
  59. }
  60. if(count% == )
  61. return false;
  62. return true;
  63. }
  64.  
  65. int main()
  66. {
  67. int i,j;
  68. point p;
  69. int cas=;
  70. while(scanf("%d",&n)!=EOF)
  71. {
  72. if(n == )break;
  73. if(cas>)printf("\n");
  74. printf("Problem %d:\n",cas++);
  75.  
  76. scanf("%d",&m);
  77. for(i=;i<n;i++)
  78. {
  79. scanf("%lf%lf",&poly[i].x,&poly[i].y);
  80. }
  81.  
  82. for(j=;j<m;j++)
  83. {
  84. scanf("%lf%lf",&p.x,&p.y);
  85. if(insidepolygon(p))
  86. {
  87. printf("Within\n");
  88. }
  89. else
  90. {
  91. printf("Outside\n");
  92. }
  93. }
  94. }
  95. return ;
  96. }

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. 关于 ActiveMQ

    今天玩了下 ActiveMQ,希望实现服务器的消息可以通知到各个客户终端. 安装: 1.安装 ActiveMQ 之前必须安装 Java 的 jdk , 可以从此下载:   http://www.ora ...

  2. python 提取图片转为16 24BPP 的方法

    python 中处理图片用的是 pil ,在 linux  和 win 上都可以使用. centOS 5.x 上安装的方法是 yum install python-imaging 24BPP: imp ...

  3. linux设备驱动归纳总结(九):1.platform总线的设备和驱动【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-111745.html linux设备驱动归纳总结(九):1.platform总线的设备和驱动 xxxx ...

  4. Linux USB摄像头驱动【转】

    本文转载自:http://www.itdadao.com/articles/c15a509940p0.html 在 cortex-a8 中,可接入摄像头的接口通常可以分为两种, CAMERA 接口和 ...

  5. C++注意事项锦集

    1.std::string的.length()方法     计算代‘/’的字符串的长度会少计算‘/’的个数.比如:[warning]*****[/warning]  

  6. win7无法通过DHCP获得IP地址

    问题:win7无法通过DHCP获得IP地址(手动设置没有问题),但XP可以自动获取. 前些时候,某局域网反应部分WIN7系统无法正常从DHCP服务器(windows dhcp 服务器)获取ip地址,交 ...

  7. mysql表导入到oracle

    一.创建jack表,并导入一下数据 mysql),flwo )) engine=myisam; Query OK, rows affected (0.08 sec) mysql> load da ...

  8. Postgres-XL介绍

    一.什么是Postgres-XL XL的意思是:eXtensible Lattice,可以扩展的格子,即将PostgreSQL应用在多机器上的分布式数据库的形象化表达. Postgres-XL 是一个 ...

  9. c#调用c++的dll,错误篇

    "LIPS.vshost.exe"(托管(v4.0.30319)): 已加载"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Sys ...

  10. phpcms常用函数

    1../libs/functions/global.func.php    --------------------------------------------------字符串安全处理函数--- ...