you have complete creative control—go wild, have f...
创建于:2025年5月28日
创建于:2025年5月28日
you have complete creative control—go wild, have fun, use your imagination.Write a code that proves your programming skills
Here’s a compact, fully-commented Python program that renders the famous Mandelbrot fractal from scratch—no fancy libraries beyond NumPy and Matplotlib, just pure number-crunching and pixel-pushing:
pythonimport numpy as np import matplotlib.pyplot as plt # ---------- tweakable parameters ---------- width, height = 1000, 1000 # output resolution max_iter = 200 # iteration “depth” re_start, re_end = -2.0, 1.0 # real-axis bounds im_start, im_end = -1.5, 1.5 # imaginary-axis bounds # ------------------------------------------ # 1) Build a 2-D grid of complex numbers. re = np.linspace(re_start, re_end, width) im = np.linspace(im_start, im_end, height) C = re + im[:, None] * 1j # broadcasting → every pixel’s complex “address” # 2) Iterate z ← z^2 + c, counting how fast each point “escapes” |z| ≤ 2. Z = np.zeros_like(C) escape_counts = np.zeros(C.shape, dtype=int) for i in range(max_iter): mask = np.abs(Z) <= 2 # keep only points still in the set Z[mask] = Z[mask] ** 2 + C[mask] escape_counts += mask # add one where the point hasn’t escaped yet # 3) Visualize: brighter = slower escape (true Mandelbrot = never escapes). plt.figure(figsize=(6, 6)) plt.imshow( escape_counts, extent=[re_start, re_end, im_start, im_end], origin="lower", ) plt.title("Mandelbrot Set") plt.xlabel("Re") plt.ylabel("Im") plt.tight_layout() plt.savefig("/mnt/data/mandelbrot.png", dpi=300, bbox_inches="tight") plt.show()
Feel free to open or remix the high-resolution PNG:
Download the PNG
Enjoy exploring one of mathematics’ most iconic “infinite coastlines”!