斐波那契向日葵 Tkinter

Fibonacci Sunflower Tkinter

我正在尝试使用 tkinter 绘制斐波那契向日葵。它绘制正确,但我也希望能够绘制螺旋线。但是,我不知道如何正确连接它们。有什么想法吗?

drawing

这是我的代码:

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,...

下面这个版本的你的程序其实是画线的,但是我还没有找到21和34步的动机。我怀疑这与您在 s5 函数('5')中使用的数字有关。

import math
from Tkinter import *

class Fibonacci():
    def s5(self, 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

    def pol2cart(self, r, theta):
        x = r * math.cos(math.radians(theta))
        y = r * math.sin(math.radians(theta))
        return x,y

    def calculate_coordinates(self, num_points = 200, distance = 15):
        # do the cartesian conversion
        self.coordinates = [self.pol2cart(r, t) for r, t in self.s5(num_points, distance)]

        # center for the canvas
        self.coordinates = [(x+250,y+250) for x, y in self.coordinates]

    def plot_numbers(self, canvas):
        h = 1
        self.calculate_coordinates(num_points = 200, distance = 15)
        for x, y in self.coordinates:
            canvas.create_oval(x+7, y+7, x-7, y-7)
            canvas.create_text(x, y, text = h)
            h += 1

    def plot_lines(self, canvas):
        for delta in [21, 34]:
            for start in range(34):
                x0, y0 = self.coordinates[0]
                i = start
                while i < len(self.coordinates):
                    x1, y1 = self.coordinates[i]
                    canvas.create_line(x0, y0, x1, y1)
                    x0 = x1; y0 = y1
                    i += delta

    def create_gui(self):
        master = Tk()
        canvas = Canvas(master, width = 500, height = 500)
        canvas.pack()

        self.plot_numbers(canvas)
        self.plot_lines(canvas)

        mainloop()

def main():
    f = Fibonacci()
    f.create_gui()
    return 0

if __name__ == '__main__':
    main()

要消除以中心为起点,修改plot_lines如下:

def plot_lines(self, canvas):
    for delta in [21, 34]:
        for start in range(34):
            x0, y0 = self.coordinates[start]
            print x0, y0
            i = start + delta
            while i < len(self.coordinates):
                x1, y1 = self.coordinates[i]
                canvas.create_line(x0, y0, x1, y1)
                x0 = x1; y0 = y1
                i += delta

这给出: