【问题标题】:perimeter of a polygon with given coordinates具有给定坐标的多边形的周长
【发布时间】:2017-02-23 06:21:17
【问题描述】:

我正在尝试使用给定坐标找到 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)

【问题讨论】:

  • 看起来像一个 8 点多边形。 segments() 是什么?请发布所有相关代码。还有——为什么是abs?距离已经是积极的。另外 - 你的缩进似乎是错误的。最后的print 不应缩进。
  • 我对 python 很陌生,我做了这些更改,但我不知道如何将列表元素定义为 poly[x,y] def perimeter(poly): """A 序列的 x 和 y (x,y) 数字坐标对 """ 返回 (sum(math.hypot(x0-x1,y0-y1) for ((x0, y0), (x1, y1)) print (perimeter(poly))
  • 您遇到什么错误?什么线?仅仅说你不断收到错误是没有信息的。 segments() 是什么?如果没有看到该代码,任何人都很难说什么(除了缩进问题)。
  • 我在定义 poly[x,y] 时遇到错误我希望将列表中的点定义为 x 和 y,以便它们可以在等式中使用。 segements 功能现已被移除
  • 如果函数segments() 已被删除,那么该代码会给您一个尝试使用该函数的错误(在您的return 行中)。请向我们展示您的错误的整个追溯。

标签: python


【解决方案1】:

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)

【讨论】:

    猜你喜欢
    • 2013-03-15
    • 1970-01-01
    • 2019-12-27
    • 2017-03-18
    • 2011-10-15
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2014-08-19
    相关资源
    最近更新 更多