检查多边形是否在多边形内部
昨天我想检查一个点是否在多边形内,发现了这个很棒的脚本:https://github .com/tparkin/Google-Maps-Point-in-Polygon
但今天在工作时,我被告知我们的客户需要检查一个多边形是否在另一个多边形内部。我想知道是否有一个公式可以让我采用两个坐标(而不是一个来检查点),并从这两个坐标生成一个矩形并检查该矩形是否在多边形内。
我不知道我是否在问一个愚蠢的问题(高中老师曾经说过“没有愚蠢的问题,只有不问的傻瓜”),但如果你完全不明白我的意思,但是只是一点点,如果您能告诉我从哪里开始,我将不胜感激。
Yesterday I was looking to check if a point was inside a polygon and found this great script: https://github.com/tparkin/Google-Maps-Point-in-Polygon
But today at work I was told that our client needs to check if one polygon is inside another polygon. I am wondering if is there a formula where I can take, let's say, two coordinates (instead of one to check a point), and from those two coordinates generate a rectangle and check if that rectangle is inside a polygon.
I don't know if I'm asking a stupid question (a teacher in highschool used to say "there are no stupid questions, there is only fools who don't ask"), but if you don't understand me totally but just a bit, I'd be grateful if you just tell me where to start.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对每对线(每个多边形各一条)执行线相交测试。如果没有成对的线相交,并且多边形 A 的线端点之一位于多边形 B 内部,则 A 完全位于 B 内部。
以上适用于任何类型的多边形。如果多边形是凸的,您可以跳过线相交测试,只测试 A 的所有线端点都在 B 内部。
如果确实有必要,您可以使用 扫线算法。
可供参考的多边形示例:
Perform line intersection tests for each pair of lines, one from each polygon. If no pairs of lines intersect and one of the line end-points of polygon A is inside polygon B, then A is entirely inside B.
The above works for any type of polygon. If the polygons are convex, you can skip the line intersection tests and just test that all line end-points of A are inside B.
If really necessary, you can speed up the line intersection tests using the sweep line algorithm.
Example Polygons for reference:
首先使用脚本检查多边形中的一个角点是否位于另一个多边形内部。然后检查多边形中的任何直线是否与另一个多边形中的任何直线相交。如果不是,则该多边形位于另一个多边形内部。
First check that one of the corner points in the polygon is inside the other polygon using the script. Then check if any of the lines in the polygon crosses any of the lines in the other polygon. If they don't, the polygon is inside the other polygon.
多边形是凸的吗因为,如果是的话,您可以为“矩形”的两个“角”运行“多边形中的点”脚本。如果两个角都在里面,并且多边形没有向内的“曲线”,那么整个矩形不就在里面了吗?
Is the polygon convex? Because, if it is, you could just run the "point in polygon" script for both "corners" of your "rectangle." If both corners are in, and the polygon has no "curves" inward, then wouldn't the whole rectangle be in?
也许这部分代码可以帮助你:
Maybe this part of the code can help you:
我必须找到类似的解决方案。这是我到目前为止所拥有的:
google.maps.geometry.poly.containsLocation(latLng, pol )
true
计数器就会上升我的算法看起来像这样:
I had to find a similar solution. Here is what i have so far :
array[pol1cords[cord1,cord2...],pol2cords[cord1,cord2...],..]
google.maps.geometry.poly.containsLocation(latLng, pol)
true
counter would go upMy algorithm looks something like this: