-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
44 lines (33 loc) · 1.31 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import matplotlib.pyplot as plt
import numpy as np
def optimized_interactive_sierpinski_gasket(iterations, update_interval):
# Vertices of the initial triangle
vertices = np.array([[0, 0], [1, 0], [0.5, np.sqrt(3)/2]])
# Starting point
point = np.array([0.5, np.sqrt(3)/4])
# Store points for batch plotting
points = [point]
plt.ion() # Turn on interactive mode
fig, ax = plt.subplots()
ax.set_aspect('equal', adjustable='box')
for i in range(iterations):
# Choose a random vertex
vertex = vertices[np.random.randint(0, 3)]
# Move halfway from the current point to the chosen vertex
point = (point + vertex) / 2
points.append(point)
# Update the plot at specified intervals
if i % update_interval == 0:
ax.scatter(*zip(*points), s=0.1, color='blue')
plt.draw()
plt.pause(0.001) # Tiny pause for the plot to update
points = []
# Plot remaining points
ax.scatter(*zip(*points), s=0.1, color='blue')
plt.draw()
plt.ioff() # Turn off interactive mode
plt.show()
# Number of iterations and interval for updating the plot
iterations = 10000
update_interval = 100 # Update the plot every 100 iterations
optimized_interactive_sierpinski_gasket(iterations, update_interval)