Open Eliguli opened 4 months ago
Sierpinski Pentagons with Paddings
import matplotlib.pyplot as plt
import numpy as np
def draw_pentagon(ax, center, size, rotation=0):
angle = np.pi / 5 # 36 degrees in radians
vertices = []
for i in range(5):
x = center[0] + size * np.sin((2 * i + 1) * angle + rotation)
y = center[1] + size * np.cos((2 * i + 1) * angle + rotation)
vertices.append((x, y))
vertices.append(vertices[0]) # Close the pentagon
ax.plot(*zip(*vertices), 'k-')
return vertices
def draw_surrounding_pentagons(ax, center, size, pad, depth):
if depth == 0:
return
# Factor and new size calculation for the initial center pentagon
fact = center[0] * pad
new_size = size / (2 + fact + (np.sqrt(5) - 1) / 2)
vertices = draw_pentagon(ax, center, new_size)
new_size *= 0.95
# Calculate the centers and sizes for the surrounding pentagons
for i in range(5):
# Midpoint of the edge of the central pentagon
mid_x = (vertices[i][0] + vertices[i+1][0]) / 2
mid_y = (vertices[i][1] + vertices[i+1][1]) / 2
# Reflection of the center of the inner pentagon through the midpoint of each edge
new_center_x = (2 * mid_x - center[0]) * (1 + pad * 0.5)
new_center_y = (2 * mid_y - center[1]) * (1 + pad * 0.5)
# Rotation for the outer pentagon
rotation = np.pi / 5 # Rotate by 36 degrees
draw_pentagon(ax, (new_center_x, new_center_y), new_size, rotation)
# Recursive call for the next level of surrounding pentagons
draw_surrounding_pentagons(ax, (new_center_x, new_center_y), new_size, 0, depth-1)
fig, ax = plt.subplots(figsize=(12, 12))
ax.set_aspect('equal')
ax.axis('off')
# Parameters
center = (0, 0)
size = 1
pad = 0.05
depth = 4 # Number of recursive levels
draw_surrounding_pentagons(ax, center, size, pad, depth)
plt.show()
Python Code for Simple Mandelbrot Set MandelbrotSet.py
import numpy as np
import matplotlib.pyplot as plt
def mandelbrot(c, max_iter):
z = c
for n in range(max_iter):
if abs(z) > 2:
return n
z = z*z + c
return max_iter
def mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter):
r1 = np.linspace(xmin, xmax, width)
r2 = np.linspace(ymin, ymax, height)
n3 = np.empty((width, height))
for i in range(width):
for j in range(height):
n3[i, j] = mandelbrot(r1[i] + 1j*r2[j], max_iter)
return (r1, r2, n3)
def display_mandelbrot(xmin, xmax, ymin, ymax, width=800, height=800, max_iter=256):
r1, r2, n3 = mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter)
plt.imshow(n3.T, cmap='twilight', extent=[xmin, xmax, ymin, ymax])
plt.colorbar()
plt.title('Mandelbrot Set')
plt.xlabel('Re')
plt.ylabel('Im')
plt.show()
# Example usage
display_mandelbrot(-2.0, 1.0, -1.5, 1.5)
Python Jupyter Code for Mandelbrot Sets Mandelbrot.ipynb
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
def mandelbrot(c, max_iter):
z = c
for n in range(max_iter):
if abs(z) > 2:
return n
z = z*z + c
return max_iter
def mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter):
r1 = np.linspace(xmin, xmax, width)
r2 = np.linspace(ymin, ymax, height)
n3 = np.empty((width, height))
for i in range(width):
for j in range(height):
n3[i, j] = mandelbrot(r1[i] + 1j*r2[j], max_iter)
return (r1, r2, n3)
def display_mandelbrot(ax, x_center, y_center, zoom, width=800, height=800):
max_iter = int(256 * zoom)
x_width = 3.0 / zoom
y_height = 3.0 / zoom
xmin_new = x_center - x_width / 2
xmax_new = x_center + x_width / 2
ymin_new = y_center - y_height / 2
ymax_new = y_center + y_height / 2
r1, r2, n3 = mandelbrot_set(xmin_new, xmax_new, ymin_new, ymax_new, width, height, max_iter)
ax.clear()
ax.imshow(n3.T, cmap='twilight', extent=[xmin_new, xmax_new, ymin_new, ymax_new])
ax.set_title('Mandelbrot Set')
ax.set_xlabel('Re')
ax.set_ylabel('Im')
plt.draw()
# Initial display parameters
x_center, y_center, zoom = -0.5, 0.0, 1.0
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.4)
# Display the initial Mandelbrot set
display_mandelbrot(ax, x_center, y_center, zoom)
# Create sliders for zoom and translations
axzoom = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor='lightgoldenrodyellow')
zoom_slider = Slider(axzoom, 'Zoom', 1, 100, valinit=zoom, valstep=1)
axtrans_x = plt.axes([0.25, 0.25, 0.65, 0.03], facecolor='lightgoldenrodyellow')
trans_x_slider = Slider(axtrans_x, 'Translate X', -2.0, 2.0, valinit=x_center, valstep=0.01)
axtrans_y = plt.axes([0.25, 0.2, 0.65, 0.03], facecolor='lightgoldenrodyellow')
trans_y_slider = Slider(axtrans_y, 'Translate Y', -1.5, 1.5, valinit=y_center, valstep=0.01)
def update(val):
zoom = zoom_slider.val
x_center = trans_x_slider.val
y_center = trans_y_slider.val
display_mandelbrot(ax, x_center, y_center, zoom)
zoom_slider.on_changed(update)
trans_x_slider.on_changed(update)
trans_y_slider.on_changed(update)
plt.show()
Contour Set
Sierpinski Polygon Fractal
Pythagoras Tree
pythagoras_tree.py
coordinates.py
draw_tree.py