我是编程的新手,如果我的问题显而易见,请原谅我。 基本上我正在尝试创建一个用于Tkinter的基本极坐标系,但是,我遇到了一些问题。
以下是相关的代码:
def polar(self, theta, radius, xC, yC):
thetarad = -theta * (math.pi / 180)
xprime = xC + math.cos(thetarad) * radius
yprime = yC + math.sin(thetarad) * radius
return (xprime, yprime)
def draw(self, c):
# create X-coordinate guide list
xspaces = 55 # number of guides in the guide list
xgridwidth = 1.0 * self.width / xspaces
x = []
for i in range(xspaces+1):
x.append(self.left + i*xgridwidth)
# create Y-coordinate guide list
yspaces = 100 # number of guides in the guide list
ygridwidth = 1.0 * self.height / yspaces
y = []
for i in range(yspaces+1):
y.append(self.top + i*ygridwidth)
xC = x[30]
yC = y[25]
theta = 0
radius = 15
[xprime, yprime]=self.polar(theta, radius, xC, yC)
petalp1 = ( (x[xprime], y[yprime]) )
最上面和最下面的部分是我得到的错误。基本上我只需要xprime和yprime作为整数回来,这样在建立p1时它们可以和原始数字一样使用。感谢帮助。
完整错误:
Traceback (most recent call last):
File "E:FileName.py", line 34, in <module>
daisy.draw(c)
File "E:FileName2.py", line 174, in draw
petalp1 = (x[int(xprime)], y[int(yprime)])
IndexError: list index out of range
答案 0 :(得分:1)
似乎int(xprime)
大于xspaces
(值:55),int(yprime)
大于yspaces
(值:100),因此您尝试获取不存在的值列表x
(长度:55)和列表y
(长度:100)(x[int(xprime)]
和y[int(yprime)]
)