博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
判断点在多边形内
阅读量:6713 次
发布时间:2019-06-25

本文共 3175 字,大约阅读时间需要 10 分钟。

1 // Copyright 2000 softSurfer, 2012 Dan Sunday 2 // This code may be freely used and modified for any purpose 3 // providing that this copyright notice is included with it. 4 // SoftSurfer makes no warranty for this code, and cannot be held 5 // liable for any real or imagined damage resulting from its use. 6 // Users of this code must verify correctness for their application. 7  // a Point is defined by its coordinates {int x, y;} 8 //=================================================================== 9  // isLeft(): tests if a point is Left|On|Right of an infinite line.10 判断P2点在直线上(P0,P1)11 //    Input:  three points P0, P1, and P212 //    Return: >0 for P2 left of the line through P0 and P113 //            =0 for P2  on the line14 //            <0 for P2  right of the line15 //    See: Algorithm 1 "Area of Triangles and Polygons"16 inline int isLeft( Point P0, Point P1, Point P2 )17 {18     return ( (P1.x - P0.x) * (P2.y - P0.y)19             - (P2.x -  P0.x) * (P1.y - P0.y) );20 }21 //===================================================================22 射线法判断点在多边形内23 // cn_PnPoly(): crossing number test for a point in a polygon24 //      Input:   P = a point,25 //               V[] = vertex points of a polygon V[n+1] with V[n]=V[0]26 //      Return:  0 = outside, 1 = inside27 // This code is patterned after [Franklin, 2000]28 int cn_PnPoly( Point P, Point* V, int n )29 {30     int    cn = 0;    // the  crossing number counter31 32     // loop through all edges of the polygon33     for (int i=0; i
P.y)) // an upward crossing35 || ((V[i].y > P.y) && (V[i+1].y <= P.y))) { // a downward crossing36 // compute the actual edge-ray intersect x-coordinate37 float vt = (float)(P.y - V[i].y) / (V[i+1].y - V[i].y);38 if (P.x < V[i].x + vt * (V[i+1].x - V[i].x)) // P.x < intersect39 ++cn; // a valid crossing of y=P.y right of P.x40 }41 }42 return (cn&1); // 0 if even (out), and 1 if odd (in)43 44 }45 //===================================================================46 47 // wn_PnPoly(): winding number test for a point in a polygon48 // Input: P = a point,49 // V[] = vertex points of a polygon V[n+1] with V[n]=V[0]50 // Return: wn = the winding number (=0 only when P is outside)51 int wn_PnPoly( Point P, Point* V, int n )52 {53 int wn = 0; // the winding number counter54 55 // loop through all edges of the polygon56 for (int i=0; i
P.y) // an upward crossing59 if (isLeft( V[i], V[i+1], P) > 0) // P left of edge60 ++wn; // have a valid up intersect61 }62 else { // start y > P.y (no test needed)63 if (V[i+1].y <= P.y) // a downward crossing64 if (isLeft( V[i], V[i+1], P) < 0) // P right of edge65 --wn; // have a valid down intersect66 }67 }68 return wn;69 }70 //===================================================================

 

转载于:https://www.cnblogs.com/yhlx125/p/3241787.html

你可能感兴趣的文章
隐藏元素之后改变窗体大小时echarts显示异常问题
查看>>
Centos7上yum安装nagios
查看>>
【实战】多台NFS客户机挂载同一台NFS服务器时,每台客户机都能对共享文件进行读写操作...
查看>>
Shell脚本编程(上)
查看>>
Python学习笔记__1.3章 list和tuple
查看>>
自动安装red hat enterprise linux
查看>>
爱创课堂每日一题第二十一天-移动端性能优化?
查看>>
kafka学习笔记:知识点整理(二)
查看>>
MongoDB日常运维操作命令小结
查看>>
PHP描述冒泡排序和快速排序算法
查看>>
Engineer04
查看>>
安装CentOS 6.5 系统
查看>>
创建YUM仓库
查看>>
2018-05-28笔记(软件包安装和卸载)
查看>>
Spring Boot 如何极简入门?
查看>>
如下代码是不是多态,请大家看仔细
查看>>
如何使用 rsync 备份 Linux 系统的一些介绍
查看>>
RPM包管理器
查看>>
Oracle监听配置(五)--排错方法及常见错误
查看>>
Oracle 11g RAC Grid卸载
查看>>