算法复习——计算几何基础(zoj1081)

题目:

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

题解:

一道很基础的计算几何题···判断点是否在一个多边形内··方法是过该点做一条平行的射线看与多边形的交点是否为奇数个···开始的时候可以先判断下共线来优化一下···

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int R()
{
  int i=1,f=0;
  char c;
  for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
  if(c=='-')
  {
    i=-1;
    c=getchar();
  }
  for(;c>='0'&&c<='9';c=getchar())
    f=(f<<3)+(f<<1)+c-'0';
  return i*f;
}
const int N=105;
struct point 
{
  int x;
  int y;
}q[N],p;
int n,m,T;

inline point operator - (point a,point b)
{
  point t;
  t.x=a.x-b.x;
  t.y=a.y-b.y;
  return t;
}

inline int operator * (point a,point b)
{
  return a.x*b.y-a.y*b.x;
}

inline int dot(point a,point b)
{
  return a.x*b.x+a.y*b.y;
}

inline void pre()
{
  q[n]=q[0];
  int temp;
  for(int i=0;i<n;i++)
    temp+=q[i]*q[i+1];
  if(temp<=0)
  {
    reverse(q,q+n);
    q[n]=q[0];
  }
}

inline bool check(point a,point b,point c)
{
  point temp1=a-c;
  point temp2=b-c;
  if(temp1*temp2!=0)  return false;
  else
  {
    if(dot(temp1,temp2)<=0)  return true;
    else return false;
  }
}

inline bool jud()
{
  int cnt=0;
  for(int i=0;i<n;i++)
  {
    if(check(q[i],q[i+1],p))  return true;
    int t1=q[i].y-p.y;
    int t2=q[i+1].y-p.y;
    point temp1=q[i]-p;
    point temp2=q[i+1]-p;
    if((temp1*temp2>=0&&t1>=0&&t2<0)||(temp1*temp2<=0&&t1<0&&t2>=0))
      cnt++;
  }
  if(cnt%2!=0)  return true;
  else return false;
}
int main()
{
  //freopen("a.in","r",stdin);
  while(true)
  {
    n=R();
    if(n==0)  break;
    T++;
    if(T!=1)  cout<<endl;
    cout<<"Problem "<<T<<":"<<endl;
    m=R();
    for(int i=0;i<n;i++)
      q[i].x=R(),q[i].y=R();
    pre();
    for(int i=1;i<=m;i++)
    {
      p.x=R();
      p.y=R();
      if(jud())  cout<<"Within"<<endl;
      else cout<<"Outside"<<endl;
    }
  }
  return 0;
}

 

posted @ 2017-04-03 16:14  AseanA  阅读(232)  评论(0编辑  收藏  举报