https://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html

The C Code

Here is the code, for reference. Excluding lines with only braces, there are only 7 lines of code.

int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
Argument Meaning
nvert Number of vertices in the polygon. Whether to repeat the first vertex at the end is discussed below.
vertx, verty Arrays containing the x- and y-coordinates of the polygon's vertices.
testx, testy X- and y-coordinate of the test point.

The Method

I run a semi-infinite ray horizontally (increasing x, fixed y) out from the test point, and count how many edges it crosses. At each crossing, the ray switches between inside and outside. This is called the Jordan curve theorem.

The case of the ray going thru a vertex is handled correctly via a careful selection of inequalities. Don't mess with this code unless you're familiar with the idea ofSimulation of Simplicity. This pretends to shift the ray infinitesimally down so that it either clearly intersects, or clearly doesn't touch. Since this is merely a conceptual, infinitesimal, shift, it never creates an intersection that didn't exist before, and never destroys an intersection that clearly existed before.

The ray is tested against each edge thus:

  1. Is the point in the half-plane to the left of the extended edge? and
  2. Is the point's Y coordinate within the edge's Y-range?

Handling endpoints here is tricky.

PNPOLY - Point Inclusion in Polygon Test的更多相关文章

  1. PNPOLY - Point Inclusion in Polygon W. Randolph Franklin

    测试目标点是否在多边形内int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy) { int i, j, ...

  2. Determining if a point lies on the interior of a polygon

    Determining if a point lies on the interior of a polygon Written by Paul Bourke  November 1987 Solut ...

  3. [LeetCode] Convex Polygon 凸多边形

    Given a list of points that form a polygon when joined sequentially, find if this polygon is convex ...

  4. Web Application Penetration Testing Local File Inclusion (LFI) Testing Techniques

    Web Application Penetration Testing Local File Inclusion (LFI) Testing Techniques Jan 04, 2017, Vers ...

  5. 结合谷歌地图多边形(polygon)与Sql Server 2008的空间数据类型计算某个点是否在多边形内的注意事项

    首先在利用 GEOGRAPHY::STPolyFromText(@GeoStr, 4326) 这样的函数把字符串转换为Geography类型时,字符串里经纬度的顺序是 “经度[空格]纬度”,即“lon ...

  6. [svg 翻译教程]Polyline(折线)polygon(多边形)

    原文: http://tutorials.jenkov.com/svg/polygon-element.html Polyline 虽然说这个 元素我没用过,但是还是蛮强大的,也翻译下 示例 < ...

  7. [OpenGL][SharpGL]用Polygon Offset解决z-fighting和stitching问题

    [OpenGL][SharpGL]用Polygon Offset解决z-fighting和stitching问题 本文参考了(http://www.zeuscmd.com/tutorials/open ...

  8. WebGIS中基于AGS的画圆查询简析以及通过Polygon来构造圆的算法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 某个项目需求中需要在前端进行画圆查询,将圆范围上的多边形要素 ...

  9. 【ASC 23】G. ACdream 1429 Rectangular Polygon --DP

    题意:有很多棍子,从棍子中选出两个棍子集合,使他们的和相等,求能取得的最多棍子数. 解法:容易看出有一个多阶段决策的过程,对于每个棍子,我们有 可以不选,或是选在第一个集合,或是选在第二个集合 这三种 ...

随机推荐

  1. cf 323A A. Black-and-White Cube 立体构造 不知道为什么当k为奇数时构造不出来 挺有趣的题目吧

    A. Black-and-White Cube time limit per test 1 second memory limit per test 256 megabytes input stand ...

  2. 通过Jasmine和Guard自动测试JavaScript

    原文标题:Autotesting JavaScript with Jasmine and Guard 原文地址:http://edspencer.net/2013/06/15/autotesting- ...

  3. Elasticsearch中doc_value的认识

    前言:本文的目的是为后续磁盘空间利用优化做铺垫.主要知识点来源于官网文档 一.doc_value是什么 绝大多数的fields在默认情况下是indexed,因此字段数据是可被搜索的.倒排索引中按照一定 ...

  4. 使用 Rx 中预定义的 Subject

    看到一幅有趣的关于 Rx 学习的图,想知道学习 Rx 的学习曲线?不,是峭壁! 我们可以直接通过 Rx 的 Observer 来创建 Observable 对象. 但是,使用这种方式往往比较复杂,在特 ...

  5. C语言之数组

    数组 数组就是在内存空间中,开辟一个大的空间,然后再将这个大的空间均的分为若干份的小空间,每个小空间用来保存一个数据. 1). 数组的专业术语: 长度:指的能存放数据的个数 下标/索引:每一个数据所在 ...

  6. ASP.NET Web API中的Routing(路由)

    [译]Routing in ASP.NET Web API 单击此处查看原文 本文阐述了ASP.NET Web API是如何将HTTP requests路由到controllers的. 如果你对ASP ...

  7. Python print报ascii编码异常的靠谱解决办法

    之前遇到此异常UnicodeEncodeError: 'ascii' codec can't encode characters...,都是用这种方式解决:sys.setdefaultencoding ...

  8. labview 调用 matlab script的神坑! Error 1050 occurred at LabVIEW

    显示变量没有被定义,原因是clear 关键字的问题,去掉即可!!! 未找到 文件路径,定位: 文件路径中不能有中文路径

  9. Windows Server 2003下配置IIS6.0+php5+MySql5+PHPMyAdmin环境

    配置环境: 操作系统:Windows Server 2003 sp2企业版 Web服务器:系统自带的IIS6.0 所需工具: PHP:php-5.2.12-Win32.zip(官方网址:http:// ...

  10. 【原创】NSURLSession HTTPS Mutual Authentication

    1.引入<NSURLSessionDelegate>协议 2.登录验证请求 -(void)authenticate { NSURL *url = [NSURL URLWithString: ...