给定坐标下多边形的周长



我试图找到给定坐标的10点多边形的周长。

这是我目前得到的

但是

总是得到错误

poly = [[31,52],[33,56],[39,53],[41,53],[42,53],[43,52],[44,51],[45,51]]
x=row[0]
y=row[1]
``def perimeter(poly):
    """A sequence of (x,y) numeric coordinates pairs """
    return abs(sum(math.hypot(x0-x1,y0-y1) for ((x0, y0), (x1, y1)) in segments(poly)))
    print perimeter(poly)

poly是一个列表,要索引此列表中的元素,您需要提供单个索引,而不是一对索引,这就是为什么poly[x,y]是语法错误。元素是长度为2的列表。如果p是这些元素之一,那么对于该点(x,y) = (p[0],p[1])。下面的内容可能会给你一些启发。enumerate允许您同时循环索引(需要获得多边形中的下一个点)和点:

>>> for i,p in enumerate(poly):
    print( str(i) + ": " + str((p[0],p[1])))

0: (31, 52)
1: (33, 56)
2: (39, 53)
3: (41, 53)
4: (42, 53)
5: (43, 52)
6: (44, 51)
7: (45, 51)

最新更新