当前位置:网站首页>检测点是否在多边形内
检测点是否在多边形内
2022-06-25 07:16:00 【Jason?13】
检测点是否在多边形内C#版本
- 引射线法:从目标点出发引一条射线,看这条射线和多边形所有边的交点数目。如果有奇数个交点,则说明在内部,如果有偶数个交点,则说明在外部
判断一个点是否在多边形内的方法有很多,这里是射线法

在判断的点上画一条水平线,看这条线与多边形的交点,任意一边的点是奇数个,那么该点就在多边形内(因为一条水平线与多边形的交点是偶数个,如果该点在多边形内,那么它的左边或右边相交的点必定是奇数个,所以只要判断一边的情况就行了)
根据两点式可以求出水平线与多边形任意两个相邻点的交点
两点式公式:
private bool InArea(Vector2 rPlayerPos, List<Vector3> rPointList)
{
if (rPointList.Count < 3)
return false;
int nCrossings = 0;
for (int i = 0; i < rPointList.Count; i++)
{
Vector2 rPos1 = new Vector2(rPointList[i].x, rPointList[i].z);
var bTmpIndex = (i + 1) % rPointList.Count;//点P1和P2形参连线
Vector2 rPos2 = new Vector2(rPointList[bTmpIndex].x, rPointList[bTmpIndex].z);
if (rPos1.y == rPos2.y)
continue;
if (rPlayerPos.y < Mathf.Min(rPos1.y, rPos2.y))
continue;
if (rPlayerPos.y >= Mathf.Max(rPos1.y, rPos2.y))
continue;
float fX = (rPlayerPos.y - rPos1.y) * (rPos2.x - rPos1.x) / (rPos2.y - rPos1.y) + rPos1.x;
if (fX > rPlayerPos.x)
nCrossings++;
}
return (nCrossings % 2) == 1;
}
边栏推荐
- 想要软件测试效果好,搭建好测试环境是前提
- TCP stuff
- How is the ISM model analyzed?
- How to calculate the positive and negative ideal solution and the positive and negative ideal distance in TOPSIS method?
- Is there any risk in the security of new bonds
- Basic record of getting started with PHP
- Prepare these before the interview. The offer is soft. The general will not fight unprepared battles
- 软件确认测试有什么作用?确认测试报告的价格是多少?
- Almost taken away by this wave of handler interview cannons~
- Unity addressable batch management
猜你喜欢

第五天 脚本与UI系统

Deep learning series 45: overview of image restoration

VOCALOID notes

Quickly build a real-time face mask detection system in five minutes (opencv+paddlehub with source code)

How to interpret the information weight index?

家庭服务器门户Easy-Gate

How to calculate the distance between texts: WMD

Overview of image super score: the past and present life of image super score in a single screen (with core code)

在二叉树(搜索树)中找到两个节点的最近公共祖先(剑指offer)

How to calculate the correlation coefficient and correlation degree in grey correlation analysis?
随机推荐
面试前准备好这些,Offer拿到手软,将军不打无准备的仗
What is the difference between agreement and service?
《树莓派项目实战》第五节 使用Nokia 5110液晶屏显示Hello World
使用apt-get命令如何安装软件?
How to calculate the correlation coefficient and correlation degree in grey correlation analysis?
Deep learning series 48:deepfaker
How to interpret the information weight index?
Overview of image super score: the past and present life of image super score in a single screen (with core code)
How to calculate the independence weight index?
4个不可不知的采用“安全左移”的理由
Almost taken away by this wave of handler interview cannons~
Beam search and five optimization methods
钱堂教育商学院给的证券账户安全吗?能开户吗?
Data preprocessing: discrete feature coding method
How to analyze the grey prediction model?
Establish open data set standards and enable AI engineering implementation
Is there no risk in the security of new bonds
Sword finger offer (simple level)
Stm32cubemx learning (5) input capture experiment
Unity Addressable批量管理