假设有两个多边形 p1 和 p2,其中 p2 完全在 p1 内
p1 = [(0, 10), (10, 10), (10, 0), (0, 0)]
p2 = [(2, 6), (6, 6), (6, 2), (2, 2)]
degree_of_contact = 0
xyarrays = [p1,p2]
p1_degree_of_contact = 0
for x,y in xyarrays[0]:
if point_inside_polygon(x,y,xyarrays[1]):
p1_degree_of_contact += 1
p2_degree_of_contact = 0
for x,y in xyarrays[1]:
if point_inside_polygon(x,y,xyarrays[0]):
p2_degree_of_contact += 1
degree_of_contact = p1_degree_of_contact + p2_degree_of_contact
其中point_inside_polygon用于确定一个点是否在多边形内部(否则为 True,否则为 False),其中 poly 是包含多边形顶点坐标的对 (x,y) 的列表。该算法称为“射线投射法”
我希望以一种优雅的方式(行编码保存)将两个循环合二为一。