我正在尝试使用 tkinter 绘制斐波那契向日葵。它绘制正确,但我也希望能够绘制螺旋线。但是,我不知道如何正确连接它们。有什么想法吗?
这是我的代码:
import math
from tkinter import *
def s5(n,r): #works better for first direction
spirals = []
for i in range(n+1):
spirals.append(((r*(i**0.5),((i*(360)/(((5**0.5)+1)/2))%360))))
return spirals
# convert to cartesian to plot
def pol2cart(r,theta):
x = r * math.cos(math.radians(theta))
y = r * math.sin(math.radians(theta))
return x,y
# set size of fib sun
num_points = 200
distance = 15
# do the cartesian conversion
coordinates = [pol2cart(r,t) for r,t in s5(num_points,distance)]
# center for the canvas
coordinates = [(x+250,y+250) for x,y in coordinates]
# create gui
master = Tk()
canvas = Canvas(master,width = 500,height=500)
canvas.pack()
# plot points
h= 1
for x,y in coordinates:
canvas.create_oval(x+7,y+7,x-7,y-7)
canvas.create_text(x,y,text=h)
h += 1
mainloop()
这是我想要达到的结果:
这是一个有趣的问题,我只是草拟了一个可能的解决方案。您可以从 1 到 20 开始,然后将每个数字先加到 21。这意味着您应该将 1 连接到 22、22 连接到 43、43 连接到 64,...并再次将 2 连接到 23、23 到 44、...
这为您提供了踏板的一个方向。
对于另一个方向,你可以做同样的事情,但从 1 到 34 开始,每个数字加 34。这意味着您从 1 开始并向其添加 34。 1,35,69,... 2,36,70,...
这两个图显示了这些螺旋的样子:
事实上,这些数字并不神奇,它们来自斐波那契数列,并且根据螺旋线的层数,您应该检测到它。因此,您总是有数字差异,例如:0、1、1、2、3、5、8、13、21、34、55,...
我是一名优秀的程序员,十分优秀!