|
import streamlit as st |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
|
|
st.title('Plant Fractal') |
|
|
|
def generate_strange_attractor(num_points, a, b, c, d): |
|
x, y, z = 0.1, 0.0, 0.0 |
|
points = [] |
|
for i in range(num_points): |
|
x_dot = np.sin(y * a) - np.cos(x * b) |
|
y_dot = np.sin(z * c) - np.cos(y * a) |
|
z_dot = np.sin(x * d) - np.cos(z * c) |
|
x += 0.1 * x_dot |
|
y += 0.1 * y_dot |
|
z += 0.1 * z_dot |
|
points.append((x, y, z)) |
|
x, y, z = zip(*points) |
|
return (x, y, z) |
|
|
|
def generate_julia_set(num_points, c): |
|
def f(z, c): |
|
return z**2 + c |
|
x, y, z = np.zeros(num_points), np.zeros(num_points), np.zeros(num_points) |
|
z[0] = 1 |
|
for i in range(1, num_points): |
|
x[i], y[i], z[i] = f((x[i-1], y[i-1], z[i-1]), c) |
|
return (x, y, z) |
|
|
|
num_points = st.slider('How many points do you want to generate?', 1000, 100000, 10000) |
|
fractal_type = st.selectbox('Select a fractal type', ('Strange Attractor', 'Julia Set')) |
|
|
|
if fractal_type == 'Strange Attractor': |
|
a = st.slider('a', 0.0, 2.0, 1.2) |
|
b = st.slider('b', 0.0, 2.0, 0.6) |
|
c = st.slider('c', 0.0, 2.0, 1.7) |
|
d = st.slider('d', 0.0, 2.0, 1.5) |
|
x, y, z = generate_strange_attractor(num_points, a, b, c, d) |
|
fig = plt.figure() |
|
ax = fig.add_subplot(111, projection='3d') |
|
sns.lineplot(x=x, y=y, z=z, ax=ax, linewidth=1) |
|
ax.set_title('Strange Attractor Fractal') |
|
ax.set_xlabel('X') |
|
ax.set_ylabel('Y') |
|
ax.set_zlabel('Z') |
|
st.pyplot(fig) |
|
else: |
|
real_part = st.slider('Real part of c', -2.0, 2.0, 0.4) |
|
imag_part = st.slider('Imaginary part of c', -2.0, 2.0, 0.1) |
|
c = complex(real_part, imag_part) |
|
x, y, z = generate_julia_set(num_points, c) |
|
fig = plt.figure() |
|
ax = fig.add_subplot(111, projection='3d') |
|
sns.lineplot(x=x, y=y, z=z, ax=ax, linewidth=1) |
|
ax.set_title('Julia Set Fractal') |
|
ax.set_xlabel('X') |
|
ax.set_ylabel('Y') |
|
ax.set_zlabel('Z') |
|
st.pyplot(fig) |