当前位置:网站首页>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;
}
边栏推荐
- leetcode MYSQL数据库题目176
- Alternative implementation of Scrollview pull-down header amplification
- 数据库常见面试题(附答案)
- Signal works: time varying and time invariant
- C语言中通过sprintf()函数构造sql语句
- In XML layout, the button is always displayed on the top layer
- 在Activity外使用startActivity()方法报错原因与解决办法
- 2019.10.23训练总结
- 完美二叉树、完全二叉树、完满二叉树
- 1098 Insertion or Heap Sort (25 分)
猜你喜欢

Cisco ASA、FTD和HyperFlex HX的漏洞分析复现

Gmail: how to quickly read all messages

The collapsing "2.3 * 10 = 22" produced by multiplying float and int

点在多边形内外的判断

Memory layout of JVM objects

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI

阿里云防火墙配置,多种设置方式(iptables和fireward)

Pipeline details of IPC (interprocess communication)

The Stones Game【取石子博弈 & 思维】

float 与 int 相乘产生的令人崩溃的“ 2.3 * 10 = 22 ”
随机推荐
F5 BIG-IP iControl REST命令执行(CVE-2022-1388)
JVM之TLAB
2019.10.6训练总结
Caused by: org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
点在多边形内外的判断
Recyclerview refreshes blinks and crashes when deleting items
容器
如果我在北京,到哪里开户比较好?另外想问,现在在线开户安全么?
Codeforces Round #659 (Div. 2)
TLAB of JVM
A 2.5D Cancer Segmentation for MRI Images Based on U-Net
Flutter 基础组件之 Container
内网穿透工具frp使用入门
C语言中通过sprintf()函数构造sql语句
2020-9-14 广告系统入门
RecyclerView刷新闪烁与删除Item时崩溃问题
使用Rancher搭建Kubernetes集群
JNI. H description
sublime text3设置运行自己的Makefile
自定义控件之侧滑关闭 Activity 控件
