当前位置:网站首页>Judgment of points inside and outside polygon
Judgment of points inside and outside polygon
2022-06-29 10:10:00 【It's mally!】
Judgment of points inside and outside the polygon
remarks :
The main basis is 《 Introduction to algorithm competition classic training guide 》 The idea of ,2020.7.4
Ray method and rotation method are applicable to all polygons
One 、 Ray method :
From this point of view , A ray leading to infinity , Determine the position of the point according to the intersection condition
advantage : Simple , Commonly used in algorithm competitions
shortcoming : If the polygon is curved , You can't use the following code
Algorithm :
- Take the point to be judged as the starting point , Any ray , Calculate the number of intersections between the ray and the polygon
- If there are an even number of intersections , The point is outside the polygon ; otherwise , Point in polygon
- If it coincides or intersects with the endpoint of the line segment , Then you have to make complex judgments ; At this time, another ray can be taken
The following is 《 Introduction to algorithm competition classic training guide 》 Code for
const double eps=1e-8;
int dcmp(double x)
{
if(fabs(x)<eps)return 0;
else return x<0?-1:1;
}
int isPointInPolygon(Point p,Polygon poly )
{
int wn=0;
int n=v.size();
for(int i=0;i<n;++i){
if(isPointonSegment(p,poly[i],poly[(i+1)%n]))return -1;
int k=dcmp(Cross(poly[i+1]%n-poly[i],p-poly[i]));
int d1=dcmp(poly[i].y-p.y);
int d2=dcmp(poly[(i+1)%n].y-p.y);
if(k>0 && d1<=0 &&d2>0)wn++; // If the edge intersects the right end of the ray
if(k<0 && d1>0 &&d2<=0)wn--; // If the edge intersects the left end of the ray
}
}
The code uses the ray method , Let this ray be to the right , So its negative direction is to do , If the intersection is on the right , The number of intersections is marked up , If the intersection is on the left , The number of intersections is reduced . Let's take a closer look at the following analysis .
Calculate the intersection of ray and polygon , We must first rule out the case that there is no intersection , If it is shown in the figure, it is
and ( Remove the point on the edge ) The case where there is an intersection is
This is also the idea of the last two sentences of the code .
Two 、 Corner method :
Calculate the corner of each side of the polygon , Finally, it is eliminated as 0, Then the point is inside the polygon , Otherwise the point is outside the polygon
advantage : Even if the edges of a polygon are curved , This algorithm is also unaffected
shortcoming : Need to calculate a large number of inverse trigonometric functions , Not only is it slow , And it is easy to produce precision error .
Algorithm :
Add up the corners of each side of the polygon : If it is 360 degree , Then the point is in the polygon ; If it is 0 degree , The point is outside the polygon ; If it is 180 degree , Then point on the edge of the polygon ;
The inverse trigonometric function is used to find the angle directly , Poor accuracy and time-consuming
If it's a convex polygon , Just cross product
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=110;
const double pi=3.1415926;
struct point{
double x,y;
};
point poly[maxn];
int n,q;
double ang(double x1,double y1,double x2,double y2)
{
double ans=x1*x2+y1*y2;
double base=sqrt(x1*x1+y1*y1)*sqrt(x2*x2+y2*y2);
ans/=base;
return acos(ans);
}
int inpolygon(point p)
{
double angle=0;
for(int i=0;i<n;i++){
double x1=poly[i].x-p.x;
double y1=poly[i].y-p.y;
double x2=poly[(i+1)%n].x-p.x;
double y2=poly[(i+1)%n].y-p.y;
angle+=ang(x1,y1,x2,y2);
}
if(fabs(angle-2*pi)<0.000001){
return true;
}else{
return false;
}
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=0;i<n;i++){
scanf("%lf%lf",&poly[i].x,&poly[i].y);
}
point p;
for(int i=0;i<q;i++){
scanf("%lf%lf",&p.x,&p.y);
if(inpolygon(p)){
printf("Yes\n");
}else{
printf("No\n");
}
}
return 0;
}
边栏推荐
- Recyclerview refreshes blinks and crashes when deleting items
- JVM之虚拟机栈之动态链接
- 详细分析PBot挖矿病毒家族行为和所利用漏洞原理,提供蓝军详细防护建议
- float 与 int 相乘产生的令人崩溃的“ 2.3 * 10 = 22 ”
- A 2.5D Cancer Segmentation for MRI Images Based on U-Net
- Application of keil5 integrated development environment for single chip microcomputer
- Container of the basic component of the flutter
- Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
- Signal works: time varying and time invariant
- Wechat applet realizes store function
猜你喜欢
随机推荐
A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
ImageView图片填充问题
2019.11.13训练总结
Gmail:如何快速将邮件全部已读
逆向思维-小故事
JVM四种调用方法的指令
Codeforces - 1151b thinking
Wechat applet realizes store function
LiferayPortal JSONWS反序列化漏洞(CVE-2020-7961)分析
Perfect binary tree, complete binary tree, perfect binary tree
Alibaba cloud firewall configuration, multiple settings (iptables and firewall)
山科 的C语言2018练习题(电信)
Image of the basic component of the shutter
Leetcode MySQL database topic 180
单片机集成开发环境Keil5的使用
Rikka with Cake(线段树+线段树)
语言特性
2020-09-17 gateway业务流程 两个任务:referer认证和非商品模板化
图片验证码控件
Sublime Text3 set to run your own makefile










