gpt4 book ai didi

python - 将列表转换为序列以制作一个 sympy 多边形

转载 作者:太空宇宙 更新时间:2023-11-04 09:49:07 25 4
gpt4 key购买 nike

我想从点列表中创建一个 sympy 多边形。文档说它需要一系列点。我不知道如何转换。

from sympy import Point2D, Polygon

# this works
p1 = Polygon(Point2D(0,0), Point2D(1,0), Point2D(1,1), Point2D(0,1))
print(p1.area)

# this fails at the Polygon call
points=[]
points.append(Point2D(0,0))
points.append(Point2D(0,1))
points.append(Point2D(1,1))
points.append(Point2D(1,0))
t = tuple(points)
p = Polygon(t)

当它失败时,我得到这个

Traceback (most recent call last):
File "t_polygon.py", line 14, in <module>
p = Polygon(t)
File "C:\Program Files (x86)\Python\Lib\site-packages\sympy\geometry\polygon.py", line 126, in __new__
vertices = [Point(a, dim=2, **kwargs) for a in args]
File "C:\Program Files (x86)\Python\Lib\site-packages\sympy\geometry\polygon.py", line 126, in <listcomp>
vertices = [Point(a, dim=2, **kwargs) for a in args]
File "C:\Program Files (x86)\Python\Lib\site-packages\sympy\geometry\point.py", line 157, in __new__
raise ValueError('Nonzero coordinates cannot be removed.')
ValueError: Nonzero coordinates cannot be removed.

最佳答案

取而代之的是:

t = tuple(points)
p = Polygon(t)

你需要写:

t = tuple(points)
p = Polygon(*t)

关于python - 将列表转换为序列以制作一个 sympy 多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48583282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com