题目:

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=,f=;
char c;
for(c=getchar();(c<''||c>'')&&c!='-';c=getchar());
if(c=='-')
{
i=-;
c=getchar();
}
for(;c>=''&&c<='';c=getchar())
f=(f<<)+(f<<)+c-'';
return i*f;
}
const int N=;
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[];
int temp;
for(int i=;i<n;i++)
temp+=q[i]*q[i+];
if(temp<=)
{
reverse(q,q+n);
q[n]=q[];
}
} inline bool check(point a,point b,point c)
{
point temp1=a-c;
point temp2=b-c;
if(temp1*temp2!=) return false;
else
{
if(dot(temp1,temp2)<=) return true;
else return false;
}
} inline bool jud()
{
int cnt=;
for(int i=;i<n;i++)
{
if(check(q[i],q[i+],p)) return true;
int t1=q[i].y-p.y;
int t2=q[i+].y-p.y;
point temp1=q[i]-p;
point temp2=q[i+]-p;
if((temp1*temp2>=&&t1>=&&t2<)||(temp1*temp2<=&&t1<&&t2>=))
cnt++;
}
if(cnt%!=) return true;
else return false;
}
int main()
{
//freopen("a.in","r",stdin);
while(true)
{
n=R();
if(n==) break;
T++;
if(T!=) cout<<endl;
cout<<"Problem "<<T<<":"<<endl;
m=R();
for(int i=;i<n;i++)
q[i].x=R(),q[i].y=R();
pre();
for(int i=;i<=m;i++)
{
p.x=R();
p.y=R();
if(jud()) cout<<"Within"<<endl;
else cout<<"Outside"<<endl;
}
}
return ;
}

最新文章

  1. 【转载】shell中 dd 命令
  2. Gradle用户指南(3)-构建Java项目
  3. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏
  4. laraval框架之数据库不可不吐槽的坑
  5. Java学习--final与static
  6. 写一个“标准”宏MIN,这个宏输入两个参数并返回较小的一个。
  7. Demon_Tank (坦克移动发射子弹)
  8. DDD分层架构之领域实体(验证篇)
  9. HDOJ2005-第几天?
  10. 封装qq分享静态库到cocopod
  11. Python_格式化字符
  12. 【转】前端框架天下三分:Angular React 和 Vue的比较
  13. SQL语句(六)成批导入导出数据
  14. 洛谷P2178 品酒大会【后缀数组】【单调栈】
  15. hdu 4349 求C(n,0),C(n,1),C(n,2)...C(n,n).当中有多少个奇数 (Lucas定理推广)
  16. inviteflood 洪泛滥工具
  17. JavaWeb—Session与Cookie
  18. iClap分享:如何优雅的在 APP 中实现测试?
  19. Nordic官方网络资源介绍(官网/devzone/GitHub)
  20. anaconda安装出现failed to create anacoda menue

热门文章

  1. Win7下vc++6.0打开项目出现问题的解决方案
  2. SAP C/4HANA到底包含哪些产品?
  3. Mybatis Cache 缓存策略
  4. UVA Live 3713 Astronauts (2-SAT)
  5. Java加腾讯云实现短信验证码功能
  6. CPP-基础:C++拷贝构造函数详解
  7. vc生产垃圾清理
  8. 分享一个Delphi跨平台Http库的封装,一个Delphi跨平台TCP库的封装
  9. tomcat假死现象(转)
  10. java在线聊天项目 swt可视化窗口Design 好友列表窗口