-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiral_generator_Galsactic_Spiral.py
189 lines (141 loc) · 6.45 KB
/
Spiral_generator_Galsactic_Spiral.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import cairosvg
# Define the functions i(x) and j(x)
def i(x):
result = 0
for n in range(1, int(x) + 1):
result += np.sin((np.pi / 2) * np.floor(np.sqrt(4 * n - 3)))
return result
def j(x):
result = 0
for n in range(1, int(x) + 1):
result += np.cos((np.pi / 2) * np.floor(np.sqrt(4 * n - 3)))
return result
# Function to generate the spiral mosaic
def generate_mosaic():
Nsteps = int(concentric_rectangles_entry.get())
Xblocks = int(x_blocks_entry.get())
YBlocks = int(y_blocks_entry.get())
Xgap = int(x_gap_entry.get())
Ygap = int(y_gap_entry.get())
# Create a new figure with the desired size in inches (300x300 mm)
fig, ax = plt.subplots(figsize=(300/25.4, 300/25.4), dpi=300)
# Generate the points for a logarithmic spiral (simplified galactic spiral)
a = 0.1 # Adjust the scale parameter as needed
b = 0.2 # Adjust the shape parameter as needed
n_points = 1000 # Number of points to generate
theta_values = np.linspace(0, 10 * np.pi, n_points) # Adjust the range of theta as needed
r_values = a * np.exp(b * theta_values)
x_values = r_values * np.cos(theta_values)
y_values = r_values * np.sin(theta_values)
# Create a single plot to combine all concentric rectangles in mm
plt.figure(figsize=(15, 15)) #mm
# Create a loop to draw concentric rectangles with spirals
for step in range(Nsteps):
Xo = Xblocks - step * 2 # Number of times to replicate along the x-axis
Yo = YBlocks - step * 2 # Number of times to replicate along the y-axis
# Calculate the size of the individual pattern
pattern_width = max(x_values) - min(x_values)
pattern_height = max(y_values) - min(y_values)
# Calculate the total width and height of the mosaic
mosaic_width = Xo * pattern_width + (Xo - 1) * Xgap
mosaic_height = Yo * pattern_height + (Yo - 1) * Ygap
# Create the rectangle as a list of points along the outline
rectangle_x = []
rectangle_y = []
# Top edge of the rectangle
for x in np.linspace(-mosaic_width / 2, mosaic_width / 2, n_points):
rectangle_x.append(x)
rectangle_y.append(mosaic_height / 2)
# Right edge of the rectangle
for y in np.linspace(-mosaic_height / 2, mosaic_height / 2, n_points):
rectangle_x.append(mosaic_width / 2)
rectangle_y.append(y)
# Bottom edge of the rectangle
for x in np.linspace(mosaic_width / 2, -mosaic_width / 2, n_points):
rectangle_x.append(x)
rectangle_y.append(-mosaic_height / 2)
# Left edge of the rectangle
for y in np.linspace(mosaic_height / 2, -mosaic_height / 2, n_points):
rectangle_x.append(-mosaic_width / 2)
rectangle_y.append(y)
# Plot the mosaic without connecting lines (spiral outline)
current_spiral = []
for x, y in zip(x_values, y_values):
current_spiral.append((x, y))
current_spiral = np.array(current_spiral)
# Calculate the offset for centering
x_offset = mosaic_width / 2
y_offset = mosaic_height / 2
# Replicate the spiral pattern along the rectangle outline (blue spirals)
for _ in range(Xo):
plt.plot(current_spiral[:, 0] + x_offset, current_spiral[:, 1] + y_offset, 'b-')
x_offset -= pattern_width + Xgap
for _ in range(Yo):
plt.plot(current_spiral[:, 0] + x_offset, current_spiral[:, 1] + y_offset, 'b-')
y_offset -= pattern_height + Ygap
for _ in range(Xo):
plt.plot(current_spiral[:, 0] + x_offset, current_spiral[:, 1] + y_offset, 'b-')
x_offset += pattern_width + Xgap
for _ in range(Yo):
plt.plot(current_spiral[:, 0] + x_offset, current_spiral[:, 1] + y_offset, 'b-')
y_offset += pattern_height + Ygap
# Remove the axis labels
plt.xlabel("")
plt.ylabel("")
plt.axis('equal')
# Display the plot in the Tkinter window
canvas = FigureCanvasTkAgg(plt.gcf(), master=window)
canvas_widget = canvas.get_tk_widget()
canvas_widget.grid(row=8, column=0, columnspan=5)
# Save the Matplotlib figure as an SVG file
svg_filename = "concentric_rectangles.svg"
plt.savefig(svg_filename, format="svg", bbox_inches="tight", dpi=300)
plt.close()
# Convert the SVG file to PDF (if needed)
pdf_filename = "concentric_rectangles.pdf"
cairosvg.svg2pdf(url=svg_filename, write_to=pdf_filename)
# Cleanup the temporary SVG file
import os
#os.remove(svg_filename)
# Display a message
result_label.config(text=f"Mosaic saved as {pdf_filename}")
# Create the main window
window = tk.Tk()
window.title("Concentric Rectangles with Spirals")
# Create and place labels and entry fields for input
concentric_rectangles_label = ttk.Label(window, text="Number of Concentric Rectangles:")
concentric_rectangles_label.grid(row=0, column=0)
concentric_rectangles_entry = ttk.Entry(window)
concentric_rectangles_entry.grid(row=0, column=1)
concentric_rectangles_entry.insert(0, "3")
x_blocks_label = ttk.Label(window, text="X Blocks:")
x_blocks_label.grid(row=1, column=0)
x_blocks_entry = ttk.Entry(window)
x_blocks_entry.grid(row=1, column=1)
x_blocks_entry.insert(0, "30")
y_blocks_label = ttk.Label(window, text="Y Blocks:")
y_blocks_label.grid(row=2, column=0)
y_blocks_entry = ttk.Entry(window)
y_blocks_entry.grid(row=2, column=1)
y_blocks_entry.insert(0, "15")
x_gap_label = ttk.Label(window, text="X Gap:")
x_gap_label.grid(row=3, column=0)
x_gap_entry = ttk.Entry(window)
x_gap_entry.grid(row=3, column=1)
x_gap_entry.insert(0, "1")
y_gap_label = ttk.Label(window, text="Y Gap:")
y_gap_label.grid(row=4, column=0)
y_gap_entry = ttk.Entry(window)
y_gap_entry.grid(row=4, column=1)
y_gap_entry.insert(0, "1")
generate_button = ttk.Button(window, text="Generate Mosaic", command=generate_mosaic)
generate_button.grid(row=5, column=0, columnspan=2)
result_label = ttk.Label(window, text="")
result_label.grid(row=7, column=0, columnspan=2)
# Run the Tkinter main loop
window.mainloop()