0

我似乎无法绘制一个输入周期数的旋风。该代码应该让用户单击一个起点和终点,然后将使用从起点到终点的输入循环绘制一个旋风。

from math import *
from graphics import *

def main():
    win = GraphWin("Drawing a Cyclone",600,450)            
    win.setCoords(0,0,600,450)                             
    msg = "Please enter the number of cycles, and click the start and end positions in the window."
    Text(Point(300, 438), msg).draw(win)
    Text(Point(65,415),"# of cycles:").draw(win)
    inbox =Entry(Point(130,415),5)
    inbox.draw(win)


    start=win.getMouse()
    start.setFill("red")
    start.draw(win)
    stop=win.getMouse()
    stop.setFill("red")
    stop.draw(win)

    cycles=eval(inbox.getText())
    radius = 0                                                 
    length = sqrt((stop.x-start.x)**2+(stop.y-start.y)**2)  
    step_radius = length / (120*cycles)
    radian = atan((stop.y-start.y)/(stop.x-start.x))
    initial_angle = int(degrees(radian))

    for i in (0, cycles*360, 3):
        radius = radius + step_radius
        theta=radians(initial_angle + (360*cycles*i)+3*i)
        stop.x = start.x + radius*cos(theta)
        stop.y = start.y + radius*sin(theta)
        line=Line(start,stop)
        line.draw(win)
        start.x=stop.x
        start.y=stop.y


    win.getMouse()
    win.close()

main()

这就是我得到的

这是我应该得到的

4

1 回答 1

2

我想说你的大部分问题都围绕着这段代码:

theta=radians(initial_angle + (360*cycles*i)+3*i)
stop.x = start.x + radius*cos(theta)
stop.y = start.y + radius*sin(theta)

i变量已经在跟踪圆,所以theta应该是弧度等效于i调整初始角度,但你已经用i. 此外,停止位置应该是针对图形原点校正的cos()sin()计算的函数,但您使用start的是最初的原点,但它是循环内的移动目标。

进行调试的一种方法是将周期设置为 1 并设置radius = length而不是radius += step_radius. 这将为您提供一个简单的圆来解决正确定位、半径等问题,而不会增加螺旋的复杂性。然后加回螺旋。

这些方面的更多内容可能会给您一些解决问题的想法:

from math import *
from graphics import *

DEGREES_PER_STEP = 3

win = GraphWin("Drawing a Cyclone", 600, 450)
win.setCoords(0, 0, 600, 450)
msg = "Please enter the number of cycles, and click the start and end positions in the window."
Text(Point(300, 438), msg).draw(win)
Text(Point(65, 415), "# of cycles:").draw(win)
inbox = Entry(Point(130, 415), 5)
inbox.draw(win)

origin = win.getMouse()
origin.setFill("green")
origin.draw(win)
target = win.getMouse()
target.setFill("red")
target.draw(win)

cycles = float(inbox.getText())
radius = 0
length = sqrt((target.x - origin.x) ** 2 + (target.y - origin.y) ** 2)
step_radius = length / (cycles * 360 / DEGREES_PER_STEP)
initial_angle = atan((target.y - origin.y) / (target.x - origin.x))

start = Point(origin.x, origin.y)

for i in range(0, int(cycles * 360), DEGREES_PER_STEP):
    radius += step_radius
    theta = radians(i) + initial_angle  # initial angle already in radians
    stop = Point(origin.x + radius * cos(theta), origin.y + radius * sin(theta))
    line = Line(start, stop)
    line.draw(win)
    start = stop

win.getMouse()
win.close()

这仍然存在在某些象限中正确获取初始角度的问题,但这是您可以解决的问题。此外,如果您单击正确的点,您可以生成除以零,因此您也需要处理它:

在此处输入图像描述

请注意,虽然from graphics import *通常这样做,但from math import *风险更大——例如,math.pow()覆盖 Python 的内置pow()并且两者并不相同。考虑更安全的进口。

于 2017-02-01T19:07:37.180 回答