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.

最新文章

  1. javascript Date 总结
  2. 2.7---判断链表是否是回文(CC150)
  3. hdu 4784 Dinner Coming Soon(spfa + 优先队列)
  4. Maven 介绍
  5. Struts2中属性驱动与模型驱动
  6. Android开发之单例模式
  7. 启动php-fpm时报错
  8. iOS安全攻防(二十三):Objective-C代码混淆
  9. [原创] Assistant editor 取消拖拽 binding 的 UI 元素
  10. win7下wordPress本地搭建博客详解(深度亲测整理---傻瓜式详细教程)
  11. Android Studio 错误集
  12. Java高效计数器
  13. Html5五子棋
  14. Beta答辩总结
  15. CS DevExpress程序启动(主窗体初始化优化)
  16. 简易机器学习代码(LR,Kmeans,NN,RNN)
  17. 单片机的编程语言和开发环境 LET′S TRY“嵌入式编程”: 3 of 6
  18. 1_02 Vue Slot
  19. [PHP]json_encode增加options参数后支持中文
  20. vue生命周期简介

热门文章

  1. 如何Windows系统中搭建php环境
  2. javaapplicationWeb application setup on Ubuntu VPS
  3. Andy的生活
  4. [HMLY]10.深入研究Block用weakSelf、strongSelf、@weakify、@strongify解决循环引用
  5. JS判断在哪个浏览器打开
  6. python——爬虫
  7. 学习Sass(二)
  8. zabbix 安装配置以及漏洞检测脚本
  9. C++类继承中,基类/当前对象属性/当前对象的构造顺序
  10. Egret 位图,纹理,添加背景 学习