2013-03-06 102 views
0

结合两个循环,其中P2是完全内部的P1在假设有两个多边形P1和P2蟒蛇

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是决定是否有一个点在内部(真,假以其他方式)的多边形, 其中poly是包含多边形顶点坐标的对(x,y)列表。该算法被称为“光线投射法

我希望以一种优雅的方式(线路编码保存)这两个循环结合在一个

+0

由于我认为你的工作是相关的,看看numpy已经建立了对向量和矩阵的支持。你可以在那里做很好的技巧(比如向量化的循环),它们纯粹用C语言编写,而且速度非常快。为了提高速度:有很多内置的数学方法可以让你的生活更轻松问题。 http://www.scipy.org/Tentative_NumPy_Tutorial – entropiece 2013-03-06 18:13:15

回答

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 
1
degree_of_contact = sum(point_inside_polygon(x, y, i) for i, j in ((p1, p2), (p2, p1)) for x, y in j)