[英]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.