简体   繁体   English

简单的多边形测试,边缘表现一致

[英]Easy point-in-polygon-test with consistant behaviour for edges

While searching for point-in-polygon tests I've found the one described by Dean Povey here . 在搜索多边形测试时,我发现了Dean Povey 在此处描述的测试。 I like the approach as it can be easily understood (raycast along the x-axis) but I've come across a small inconsistency with that method. 我喜欢这种方法,因为它很容易理解(沿x轴进行射线投射),但是我遇到了与该方法的微小矛盾之处。 While writing Unit-Tests for my implementation I noticed that when taking a square as a "Test-Polygon" points on the left and bottom edge of the square are recognized as part of the polygon while points on the right and top side of the polygon are recognized as outside the polygon. 在为实现编写单元测试时,我注意到以正方形作为“测试多边形”时,正方形左侧和底部边缘的点被识别为多边形的一部分,而多边形右侧和顶部的点被识别为多边形的一部分。被识别为多边形外部。 Here's a small drawing showing what I mean (+ are not recognized as inside, doubled lines and the x are): 这是一张小图,显示了我的意思(+未被识别为内部,双倍线和x为):

+--------+
‖        |
‖        |
x========+

Does anyone know how I can change the algorithm to show consistent behaviour for points on the edge? 有谁知道我可以如何更改算法以显示边缘点的一致行为? It doesn't matter if the edges are considered as inside or not, just that the behaviour is consistant. 边缘是否视为内部并不重要,只是行为是否一致。

To test if a point P lies on an edge QR , consider the transformation that maps Q to the origin and R to the point (0, 1) . 要测试点P位于边缘QR ,请考虑将Q映射到原点并将R映射到点(0, 1)的变换。

Using complex numbers, write this transformation as Z = (z - Q) / (R - Q) and transform P with (P - Q) / (R - Q) . 使用复数,将此变换写为Z = (z - Q) / (R - Q)然后用(P - Q) / (R - Q)变换P This number must be pure real and its real part be in [0, 1] . 该数字必须是纯实数,并且其实数部分应位于[0, 1]

You can use this test to detect points on the outline. 您可以使用此测试来检测轮廓上的点。 It is also possible to allow a tolerance, by allowing the imaginary part to be a small number. 通过允许虚部很小,也可以允许公差。 In this case, it is advisable to normalize the denominator R - Q , so that the imaginary part becomes the Euclidean distance to the segment. 在这种情况下,建议对分母R - Q进行归一化,以使虚部成为到该段的欧几里得距离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM