简体   繁体   English

如何判断多边形是否在Python中的另一个多边形内?

[英]How to judge if a polygon is inside another polygon in Python?

I need to define a Python function which can dectect if a convex polygon (polygon a) is inside another polygon(polygon b). 我需要定义一个Python函数,它可以检测凸多边形(多边形a)是否在另一个多边形(多边形b)内。

The vertex of each polygon are given. 给出每个多边形的顶点。

(only use basic lib as well as numpy) (只使用基本库以及numpy)

It's easy for a human to make a judgement with their eyes. 人类很容易用眼睛做出判断。 But I have no idea how to describe the method in Python. 但我不知道如何在Python中描述该方法。 I have tried to check some source code of lib (like shapely) but can't understand how it works. 我试图检查一些lib的源代码(如形状),但无法理解它是如何工作的。 ''' “””

def isinside(polya, polyb):
     #Polya: [(x1,y1), (x2,y2), (x3,y3),...]
     #Polyb: [(x1,y1), (x2,y2), (x3,y3),...]
    #if polya inside polyb
     return True
     # else 
     return False

''' Could someone please give some advice or show some codes? '''有人可以提供一些建议或显示一些代码吗? Thanks! 谢谢!

You could use the shapely lib for that. 您可以使用shapely lib。 It'd go like 它会像

from shapely.geometry import Polygon

polya = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) 
polyb = Polygon([(0.5, 0.5), (0.5, 0.8), (0.8, 0.8), (0.8, 0.5)]) 

polya.contains(polyb)
# True

This module has got a lot more on most geometry related operations, so refer to their User Manual for further examples and thorough explanations. 该模块在大多数几何相关操作中有更多功能,因此请参阅其用户手册以获取更多示例和详尽说明。

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

相关问题 如何获取python中多边形内的点列表? - How to get list of points inside a polygon in python? 如何使用python检查多边形内像素的颜色并删除包含白色像素的多边形? - How to check the color of pixels inside a polygon and remove the polygon if it contains white pixels using python? 如何获取Python中多边形内网格点的坐标? - How to get the coordinates of grid points inside a polygon in Python? 如何识别一个点(由纬度和经度创建)是否在Python中的多边形内部或交叉? - How to identify if a point (created by latitude and longitude) is inside or cross a polygon in Python? 如何在Python中从一个多边形移动到另一个多边形? - how to move from one polygon to another in file in python? 如何从具有空白遮罩或多边形内部的多边形生成遮罩? - How to generate mask from polygons with blank mask or polygon inside polygon? 如何在Python中计算多边形的质心 - How to compute the centroid of a polygon in Python 查找多边形内最大的矩形 - Python - Find largest rectangle inside polygon - Python python 点内多边形(点云数据) - python point inside polygon (point cloud data) 如何在不规则多边形内找到一个点 - How to find a point inside an irregular polygon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM