点与多多边形的关系
2018-03-14 17:33
99 查看
问题点的位置
二维平面上会给出以N个点构成的多边形。当平面上面给出点 P1, P2时,请编写出判断点 P1, P2是否在多边形外部,或者是在内部的程序。点 P1, P2 不会在多边形的边上面。
![](//img-blog.csdn.net/20180314173118238)
输入
第一行给出构成多边形的个数(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;
}
二维平面上会给出以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;
}
相关文章推荐
- 判断点与多边形的关系(1):向量积法
- 通过C++实现判断点与多边形的关系和两点之间的距离
- Broken line - SGU 124(判断点与多边形的关系)
- 判断点与多边形的关系(5):OpenCV交互式绘制图像ROI应用代码
- 判断点与多边形的位置关系
- 几种判断点与多边形关系的算法介绍
- zoj1081(点与多边形的位置关系)
- poj 1584 A Round Peg in a Ground Hole 多边形凸性判断,点与多边形位置关系,圆与多边形位置关系
- c# 判断点与圆、矩形、多边形的关系
- 正多边形顶点、面数、棱数的关系(欧拉定理)
- 判断点与多边形的位置关系
- 判断点与多边形的位置关系
- 判断点与多边形的关系(2):面积法
- A Round Peg in a Ground Hole(判断是否是凸包,点是否在凸包内,圆与多边形的关系)
- 判断点与多边形的关系(3):角度和法
- LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)
- GIS应用案例2:分析多边形的邻接关系
- POJ1584 几何 凸包判断 点和多边形的位置关系
- UVA 10256 The Great Divide (凸包,多边形的位置关系)
- Cupid's Arrow---hdu1756(判断点与多边形的位置关系 模板)