some new stuff.
idk its all pretty fun! some C++ too!
This commit is contained in:
60
cube/fibonnacci.py
Normal file
60
cube/fibonnacci.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import math
|
||||
import tkinter as tk
|
||||
|
||||
class Fibonacci:
|
||||
def s5(self, n, r):
|
||||
"""Generate Fibonacci spiral polar coordinates"""
|
||||
spirals = []
|
||||
phi = (1 + 5 ** 0.5) / 2 # golden ratio
|
||||
for i in range(n + 1):
|
||||
angle = (i * 360 / phi) % 360
|
||||
spirals.append((r * (i ** 0.5), angle))
|
||||
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):
|
||||
# Convert polar to Cartesian coordinates
|
||||
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):
|
||||
self.calculate_coordinates()
|
||||
for idx, (x, y) in enumerate(self.coordinates, start=1):
|
||||
canvas.create_oval(x - 7, y - 7, x + 7, y + 7, fill="white")
|
||||
canvas.create_text(x, y, text=str(idx))
|
||||
|
||||
def plot_lines(self, canvas):
|
||||
for delta in [21, 34]:
|
||||
for start in range(34):
|
||||
x0, y0 = self.coordinates[start]
|
||||
i = start + delta
|
||||
while i < len(self.coordinates):
|
||||
x1, y1 = self.coordinates[i]
|
||||
canvas.create_line(x0, y0, x1, y1)
|
||||
x0, y0 = x1, y1
|
||||
i += delta
|
||||
|
||||
def create_gui(self):
|
||||
master = tk.Tk()
|
||||
master.title("Fibonacci Spiral")
|
||||
canvas = tk.Canvas(master, width=500, height=500, bg="white")
|
||||
canvas.pack()
|
||||
|
||||
self.plot_numbers(canvas)
|
||||
self.plot_lines(canvas)
|
||||
|
||||
master.mainloop()
|
||||
|
||||
|
||||
def main():
|
||||
f = Fibonacci()
|
||||
f.create_gui()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user