Skip to content

Commit 265bfd7

Browse files
Merge pull request steam-bell-92#302 from NileshRaut2601/fibonacci-update
Improve the fibonacci curve
2 parents 2e972ce + 1ae9b30 commit 265bfd7

1 file changed

Lines changed: 149 additions & 93 deletions

File tree

Lines changed: 149 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,174 @@
1-
import time
21
import turtle
2+
import math
33

44

5-
def build_fibonacci_series(term_count):
6-
if term_count <= 0:
7-
return []
8-
9-
series = [0]
10-
11-
if term_count == 1:
12-
return series
13-
14-
series.append(1)
15-
16-
while len(series) < term_count:
17-
series.append(series[-1] + series[-2])
18-
19-
return series
20-
5+
def print_banner():
6+
print("=" * 58)
7+
print("FIBONACCI SERIES GENERATOR")
8+
print("=" * 58)
9+
print("Generate Fibonacci numbers and draw a spiral.\n")
2110

22-
def draw_dotted_arc(pen, screen, radius, extent, dot_color, dot_size=7, step_degrees=3, join_delay=0.008):
23-
steps = max(1, int(abs(extent) / step_degrees))
24-
arc_step = extent / steps
2511

26-
for _ in range(steps):
27-
pen.dot(dot_size, dot_color)
28-
pen.circle(radius, arc_step)
29-
screen.update()
30-
time.sleep(join_delay)
31-
32-
33-
def show_fibonacci_curve(series):
34-
35-
curve_terms = series[1:13] if len(series) > 13 else series[1:]
36-
if not curve_terms:
37-
curve_terms = [0]
12+
def fibonacci(n):
13+
if n <= 0:
14+
return []
15+
if n == 1:
16+
return [1]
17+
fib = [1, 1]
18+
while len(fib) < n:
19+
fib.append(fib[-1] + fib[-2])
20+
return fib
3821

39-
positive_values = [value for value in curve_terms if value > 0]
40-
max_value = max(positive_values, default=1)
41-
42-
scale = max(6, min(20, 200 // max_value if max_value else 6))
4322

44-
screen_height = 720
23+
def get_term_count():
24+
while True:
25+
raw_value = input("Enter Fibonacci terms (5-12 recommended): ").strip()
26+
try:
27+
term_count = int(raw_value)
28+
if term_count <= 0:
29+
print("Please enter a positive number.")
30+
continue
31+
return term_count
32+
except ValueError:
33+
print("Invalid input. Please enter a whole number.")
34+
35+
def build_layout(fib):
36+
squares = []
37+
38+
min_x, max_x = 0, 0
39+
min_y, max_y = 0, 0
40+
41+
cx, cy = 0, 0
42+
43+
for i in range(len(fib)):
44+
size = fib[i]
45+
direction = i % 4
46+
47+
if i == 0:
48+
x, y = cx, cy
49+
max_x = max(max_x, x + size)
50+
max_y = max(max_y, y + size)
51+
else:
52+
if direction == 0:
53+
x = prev_max_x
54+
y = prev_y + prev_size - size
55+
max_x = max(max_x, x + size)
56+
elif direction == 1:
57+
x = prev_x
58+
y = prev_max_y
59+
max_y = max(max_y, y + size)
60+
elif direction == 2:
61+
x = prev_x - size
62+
y = prev_y
63+
min_x = min(min_x, x)
64+
else:
65+
x = prev_x + prev_size - size
66+
y = prev_y - size
67+
min_y = min(min_y, y)
68+
69+
squares.append((x, y, size, direction))
70+
71+
prev_x, prev_y, prev_size = x, y, size
72+
prev_max_x, prev_max_y = x + size, y + size
73+
74+
return squares, min_x, min_y, max_x, max_y
75+
76+
def draw_grid(t, x, y, size):
77+
t.penup()
78+
t.goto(x, y)
79+
t.setheading(0)
80+
t.pencolor("#374151")
81+
t.pensize(1)
82+
83+
t.pendown()
84+
for _ in range(4):
85+
total_drawn = 0
86+
while total_drawn < size:
87+
t.forward(min(3, size - total_drawn))
88+
t.penup()
89+
t.forward(min(2, size - (total_drawn + 3)))
90+
t.pendown()
91+
total_drawn += 5
92+
t.left(90)
93+
t.penup()
94+
95+
def draw_smooth_spiral(t, squares, to_screen, scale):
96+
t.pencolor("#2563eb")
97+
t.pensize(3)
98+
t.penup()
99+
100+
for i, square in enumerate(squares):
101+
x, y, size, direction = square
102+
scaled_size = size * scale
103+
104+
if direction == 0:
105+
start_x, start_y = x, y
106+
start_heading = 0
107+
elif direction == 1:
108+
start_x, start_y = x + size, y
109+
start_heading = 90
110+
elif direction == 2:
111+
start_x, start_y = x + size, y + size
112+
start_heading = 180
113+
else:
114+
start_x, start_y = x, y + size
115+
start_heading = 270
116+
117+
sx, sy = to_screen(start_x, start_y)
118+
119+
if i == 0:
120+
t.goto(sx, sy)
121+
122+
t.setheading(start_heading)
123+
t.pendown()
124+
t.circle(scaled_size, 90)
125+
t.penup()
126+
127+
def fibonacci_spiral(n):
45128
screen = turtle.Screen()
46-
screen.title("Fibonacci Curve")
47-
screen.bgcolor("white")
48-
screen.setup(width=900, height=screen_height)
129+
screen.setup(1400, 900)
130+
screen.bgcolor("black")
131+
screen.title("Perfect Fibonacci Spiral")
49132
screen.tracer(0)
50133

51-
pen = turtle.Turtle()
52-
pen.hideturtle()
53-
pen.speed(0)
54-
pen.penup()
55-
pen.pensize(1)
134+
grid = turtle.Turtle()
135+
grid.speed(0)
136+
grid.hideturtle()
137+
138+
spiral = turtle.Turtle()
139+
spiral.speed(0)
140+
spiral.hideturtle()
56141

57-
title_y = screen_height // 2 - 40
58-
pen.goto(0, title_y)
59-
pen.color("#111827")
60-
pen.write("Fibonacci Curve", align="center", font=("Arial", 18, "bold"))
142+
fib = fibonacci(n)
143+
squares, min_x, min_y, max_x, max_y = build_layout(fib)
61144

62-
pen.goto(0, title_y - 28)
63-
pen.color("#374151")
64-
pen.write("Drawing... (click to close when finished)", align="center", font=("Arial", 10, "normal"))
145+
total_w = max_x - min_x
146+
total_h = max_y - min_y
147+
padding = 60
65148

66-
start_x = -120
67-
start_y = -120
68-
colors = ["#0ea5e9", "#10b981", "#f59e0b", "#f97316", "#8b5cf6"]
149+
scale = min((1300 - padding * 2) / total_w, (850 - padding * 2) / total_h)
69150

70-
pen.goto(start_x, start_y)
71-
pen.setheading(0)
151+
offset_x = -(min_x + total_w / 2) * scale
152+
offset_y = -(min_y + total_h / 2) * scale
72153

73-
for index, value in enumerate(curve_terms):
74-
radius = max(10, value * scale) if value > 0 else 10
75-
color = colors[index % len(colors)]
76-
pen.pencolor(color)
77-
draw_dotted_arc(pen, screen, radius, 90, color, dot_size=6, step_degrees=4, join_delay=0.004)
78-
pen.left(90)
154+
def to_screen(x, y):
155+
return (x * scale + offset_x, y * scale + offset_y)
79156

80-
pen.penup()
81-
pen.goto(0, start_y - 30)
82-
pen.color("#111827")
83-
pen.write("Done — click anywhere to close", align="center", font=("Arial", 10, "normal"))
157+
for sq in squares:
158+
x, y, size, _ = sq
159+
sx, sy = to_screen(x, y)
160+
draw_grid(grid, sx, sy, size * scale)
161+
162+
draw_smooth_spiral(spiral, squares, to_screen, scale)
84163

85164
screen.update()
86165
screen.exitonclick()
87166

88167

89168
def main():
90-
print("🔢 Fibonacci Series Generator 🔢")
91-
print("📐 Pattern: 0, 1, 1, 2, 3, 5, 8, 13...\n")
92-
93-
while True:
94-
try:
95-
term_count = int(input("➡️ Enter number of terms: "))
96-
if term_count <= 0:
97-
print("❌ Please enter a positive whole number.\n")
98-
continue
99-
break
100-
except ValueError:
101-
print("❌ Invalid input! Please enter a whole number.\n")
102-
103-
series = build_fibonacci_series(term_count)
104-
105-
print("\n✨ Fibonacci Series:")
106-
print(" → ".join(map(str, series)))
107-
print(f"\n📊 Sum of {len(series)} terms: {sum(series)}")
108-
109-
try:
110-
show_fibonacci_curve(series)
111-
except turtle.Terminator:
112-
print("\n⚠️ Turtle window was closed before drawing finished.")
113-
except Exception as error:
114-
print(f"\n⚠️ Graphical view could not be displayed: {error}")
115-
169+
print_banner()
170+
terms = get_term_count()
171+
fibonacci_spiral(terms)
116172

117173
if __name__ == "__main__":
118174
main()

0 commit comments

Comments
 (0)