import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
from mpl_toolkits.mplot3d import Axes3D
import colorsys
import random
import warnings
warnings.filterwarnings("ignore")

# -------------------------- 超复杂分形渲染 --------------------------
def mandelbrot_3d(h, w, max_iter=120):
    y = np.linspace(-1.5, 1.5, h)
    x = np.linspace(-2.0, 1.0, w)
    X, Y = np.meshgrid(x, y)
    c = X + 1j * Y
    z = np.zeros_like(c)
    div_time = np.zeros(c.shape, dtype=int)
    mask = np.ones(c.shape, dtype=bool)

    for i in range(max_iter):
        z[mask] = z[mask] ** 2 + c[mask]
        mask_new = np.abs(z) < 2
        div_time[mask & ~mask_new] = i
        mask = mask_new

    Z = np.log(div_time + 1) ** 1.4
    Z[~mask] = 0
    return X, Y, Z, div_time

# -------------------------- 彩色系统 --------------------------
def get_rgb_colors(n):
    colors = []
    for i in range(n):
        hue = (i * 0.07 + random.random() * 0.1) % 1.0
        r, g, b = colorsys.hsv_to_rgb(hue, 0.85, 0.9)
        colors.append((r, g, b))
    return colors

# -------------------------- 3D 分形 + 多层装饰图形 --------------------------
fig = plt.figure(figsize=(16, 14), dpi=120)
ax = fig.add_subplot(111, projection='3d')
ax.set_facecolor('#04071f')

# 生成复杂分形
h, w = 320, 480
X, Y, Z, div = mandelbrot_3d(h, w, max_iter=150)

# 多层叠加渲染
colors = get_rgb_colors(256)
norm = plt.Normalize(np.min(div), np.max(div))
cmap = plt.cm.plasma

# 主分形曲面
ax.plot_surface(
    X, Y, Z,
    facecolors=cmap(norm(div)),
    rstride=1, cstride=1,
    linewidth=0, antialiased=True, shade=True
)

# 随机装饰圆环
patches = []
for _ in range(180):
    cx = random.uniform(-1.8, 0.8)
    cy = random.uniform(-1.2, 1.2)
    r = random.uniform(0.01, 0.12)
    circle = Circle((cx, cy), r)
    patches.append(circle)

collection = PatchCollection(patches, alpha=0.22, color='cyan')
ax.add_collection3d(collection, zs=0, zdir='z')

# 随机星云点云
xs = np.random.normal(0, 0.8, 1200)
ys = np.random.normal(0, 0.8, 1200)
zs = np.random.uniform(0, 0.6, 1200)
colors_point = get_rgb_colors(1200)
ax.scatter(xs, ys, zs, s=1, c=colors_point, alpha=0.7)

# 螺旋线
t = np.linspace(0, 24 * np.pi, 2000)
sx = 0.9 * np.cos(t) * np.exp(-t * 0.02)
sy = 0.9 * np.sin(t) * np.exp(-t * 0.02)
sz = 0.35 * np.sin(t * 0.7)
ax.plot(sx, sy, sz, color='magenta', linewidth=0.8, alpha=0.7)

# 视角与样式
ax.view_init(elev=35, azim=60)
ax.set_title('ULTIMATE COMPLEX FRACTAL ART\nPython 最强复杂图形',
             color='white', fontsize=18, pad=20)
ax.grid(False)
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
plt.tight_layout()
plt.show()