【ZOJ 1081】Points Within 弧长法判点与多边形的位置关系

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

题意 : 有一个有  n 个点组成的多边形,接下来给出 m 个点 ,判断点是否在多边形内。

题解 :弧长法,由于只有乘法和加法,可以保证高精度。以判定点做单位圆,计算多边形在单位圆上的投影代数和,若代数和为0,则点在多边形外;若代数和为 2π , 则点在多边形内;若代数和为 π ,则点在多边形上。

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 100 + 7;
typedef struct {
  double x;
  double y;
}point;
point p1[maxn];
point p[maxn];
point b;
int kase, n, m;
int inpolygon(point t, int n)
{
  int t1,t2,sum,f;
  for(int i = 0; i <= n; i++){   // 将原点移动到 t 点
    p[i].x = p1[i].x-t.x;
    p[i].y = p1[i].y-t.y;
  }
  t1 = p[0].x>=0 ? (p[0].y >=0 ? 0 : 3) : (p[0].y >= 0 ? 1 : 2);  //判断象限;
  sum = 0;
  int i;
  for( i = 1; i <= n; i++){
    if(!p[i].x&&!p[i].y) break;  // 有个点和多边形重合
    f = p[i].y*p[i-1].x-p[i].x*p[i-1].y;
    if(!f&&p[i].x*p[i-1].x<=0&&p[i].y*p[i-1].y<=0) break;  //点在多边形某条边上
    t2 = p[i].x >= 0 ? (p[i].y >= 0 ? 0 : 3) : (p[0].y >= 0 ? 1 :2); //判断点所在的象限
    if(t2 == (t1+1)%4) sum += 1;  //下一个象限
    else if(t2 == (t1+3)%4) sum -=1; // 上一个象限
    else if(t2 == (t1+2)%4){  //对角线象限
      if(f > 0) sum += 2;
      else sum -= 2;
    }
    t1 = t2;
  }
  if(i <= n || sum) return 1;
  else return 0;
}
int main()
{
  kase = 0;
  int first = 1;
  while(scanf("%d %d",&n,&m)==2&&n){
    if(!first) printf("\n");
    for(int i = 0; i < n; i++) scanf("%lf %lf",&p1[i].x,&p1[i].y);
    p1[n] = p1[0];
    printf("Problem %d:\n",++kase);
    for(int i = 0; i < m; i++){
      scanf("%lf %lf",&b.x,&b.y);
      if(inpolygon(b,n)) printf("Within\n");
      else printf("Outside\n");
    }
    first = 0;
  }
  return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值