您的位置:首页 > 其它

点与多多边形的关系

2018-03-14 17:33 99 查看
问题点的位置
二维平面上会给出以N个点构成的多边形。当平面上面给出点 P1, P2时,请编写出判断点 P1, P2是否在多边形外部,或者是在内部的程序。点 P1, P2 不会在多边形的边上面。


输入

第一行给出构成多边形的个数(1≦N≦100,000),

第二行开始给出将输入的N个点的整数坐标(-10^9 ≦ x, y ≦ 10^9)。

多边形的坐标按顺时针方向或逆时针方向给出。

最后两行给出点 P1, P2 的坐标。

输出

第一行上,如果点 P1在多边形里面的话,输出"in",在外边的话,输出"out"。

第二行上,如果点 P2在多边形里面的话,输出"in", 在外边的话,输出"out"。

 

输入案例

41 11 33 33 10 02 2
输出案例

outin
 

#include <cstdio>
#include <vector>
#define maxfunc(a,b)(a>b?a:b)
#define minfunc(a,b)(a<b?a:b)

using namespace std;

typedef struct point{
long x, y;
}Point;

typedef struct poligon{
int ptNums;
Point *ptlst;
}Poligon;
int n,cas;
int main(){
vector<Point> vpt;
vpt.reserve(100001);
scanf("%d", &n);
for (int i = 0; i < n; i++){
Point p;

scanf("%ld%ld", &p.x, &p.y);

vpt.push_back(p);
}
Poligon pol;
pol.ptNums = n;
pol.ptlst = &vpt[0];

Point testpt;
cas = 0;
while (scanf("%ld%ld", &testpt.x, &testpt.y) == 2)
{
int cnt = 0;
for (int i = 0; i < pol.ptNums; i++){
long x1, x2, y1, y2;
double tx, t;
x1 = pol.ptlst->x, y1 = pol.ptlst->y;

if (i == (n - 1)){
// next point is first point(0) if current is n-1
pol.ptlst -= (n - 1);
}
else pol.ptlst++; // go next point
x2 = pol.ptlst->x, y2 = pol.ptlst->y;
//tmp1 = maxfunc(y1, y2);

if (testpt.y >= maxfunc(y1, y2) || testpt.y <= minfunc(y1, y2)) continue;
t = (double)(testpt.y - y2) / (y1 - y2);
// tx is the cross point
tx = (double)x2 - t*(x2 - x1);
// only count the point at left of tx
if (testpt.x < tx) cnt++;
}
printf("%s\n", cnt % 2 == 0 ? "out" : "in");
if(++cas>1) break;
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  多边形