我试着理解递归。 用旋转来描绘递归中包含的正方形,我觉得很有趣。 在
虽然可以迭代运行,但从内部调用recursive_square函数将不起作用。 在
你能解释一下为什么会变成这样,指出我从概念上忽略了什么吗? 来自tkinter导入*
导入匹配
WIDTH=600
HEIGHT=600
CANVAS_MID_X=WIDTH/2
CANVAS_MID_Y=HEIGHT/2
center=(canvas_mid_x,CANVAS_MID_Y ) ) ) ) ) ) ) )。
SIDE=WIDTH - 50
根=tk ()
geometry='%dx%d 500 100' % (WIDTH,HEIGHT )
根.几何(几何) )。
canvas=canvas (根,bg='black ',height=HEIGHT,width=WIDTH ) )
canvas.pack (
original_vertices=[
[CANVAS_MID_X - SIDE/2,CANVAS_MID_Y - SIDE/2],
[CANVAS_MID_X SIDE/2,CANVAS_MID_Y - SIDE/2],
[CANVAS_MID_X SIDE/2,CANVAS_MID_Y SIDE/2],
[CANVAS_MID_X - SIDE/2,CANVAS_MID_Y SIDE/2],
]
defrecursive_square(angle=0,points=original_vertices,center=CENTER,side=SIDE ) :
if angle 45:
Angle=math.radians(Angle ) )。
cos_val=math.cos(Angle ) )。
sin_val=math.sin(angle )
cx,cy=中心
offset=(side*math.tan(Angle )/(1math.tan (angle ) ) )
new_points=[[x,y] for x,y in points]
new_points[0][0] =offset
new_points[1][1] =offset
new_points[2][0] -=offset
new_points[3][1] -=offset
canvas.create _ polygon (new _ points,outline='red ' ) ) )。
#recursive_square(Angle5) )。
forangleinxrange (0,45,5 ) : #交互式版本
recursive_square(angle ) )。
#recursive_square ()
主loop () )