0

假设有两个多边形 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) 的列表。该算法称为“射线投射法”

我希望以一种优雅的方式(行编码保存)将两个循环合二为一。

4

2 回答 2

3

以下应该有效:

degree_of_contact = 0
for tmp1, tmp2 in [(p1, p2), (p2, p1)]:
    for x,y in tmp1:
        if point_inside_polygon(x, y, tmp2):
            degree_of_contact += 1
于 2013-03-06T18:09:05.133 回答
1
degree_of_contact = sum(point_inside_polygon(x, y, i) for i, j in ((p1, p2), (p2, p1)) for x, y in j)
于 2013-03-06T18:18:27.333 回答