Big Hero 6
Big Hero 6
Using mandelbulb fractal to create portal visual effect
import mathimport randomfrom PIL import Imageimgx = 512imgy = 512image = Image.new("RGB", (imgx, imgy))pixels = image.load()n = 8# drawing area (xa < xb & ya < yb)xa = -1.5xb = 1.5ya = -1.5yb = 1.5maxIt = 256 # max number of iterations allowedpi2 = math.pi * 2.0# random rotation angles to convert 2d plane to 3d planexy = random.random() * pi2xz = random.random() * pi2yz = random.random() * pi2sxy = math.sin(xy) ; cxy = math.cos(xy)sxz = math.sin(xz) ; cxz = math.cos(xz)syz = math.sin(yz) ; cyz = math.cos(yz)origx = (xa + xb) / 2.0 ; origy = (ya + yb) / 2.0for ky in range(imgy): b = ky * (yb - ya) / (imgy - 1) + ya for kx in range(imgx): a = kx * (xb - xa) / (imgx - 1) + xa x = a ; y = b ; z = 0.0 # 3d rotation around center of the plane x = x - origx ; y = y - origy x0=x*cxy-y*sxy;y=x*sxy+y*cxy;x=x0 # xy-plane rotation x0=x*cxz-z*sxz;z=x*sxz+z*cxz;x=x0 # xz-plane rotation y0=y*cyz-z*syz;z=y*syz+z*cyz;y=y0 # yz-plane rotation x = x + origx ; y = y + origy cx = x ; cy = y ; cz = z for i in range(maxIt): r = math.sqrt(x * x + y * y + z * z) t = math.atan2(math.hypot(x, y), z) p = math.atan2(y, x) rn = r ** n x = rn * math.sin(t * n) * math.cos(p * n) + cx y = rn * math.sin(t * n) * math.sin(p * n) + cy z = rn * math.cos(t * n) + cz if x * x + y * y + z * z > 4.0: break pixels[kx, ky] = (i % 4 * 64, i % 8 * 32, i % 16 * 16)image.save("Mandelbulb.png", "PNG")