Mandelbrot Set with Different Paramaters (Source Code)

Using the center coordinate and zoom factor to draw the following Mandelbrot Set visualizations.

(-0.75, 0), Zoom=1
(-0.103865, -0.9584393), Zoom=167466.7.
(-0.761574, -0.0847596) Zoom=3125
(-0.761574, -0.0847596) Zoom=78125
(-0.59990625, -0.4290703125) Zoom=1024
(-1.62917, -0.0203968) Zoom=3125
(0.2613577, -0.002018128) Zoom=3354.786

Source Code:

from PIL import Image
import colorsys
import math

px, py = -0.7746806106269039, -0.1374168856037867 #Tante Renate
px, py, zoom = -0.74384935657398, -0.13170134084746293, 5788441.443619884
px, py, zoom = 2.613577e-1, -2.018128e-3, 3.354786e+3
px, py, zoom = -0.59990625, -0.4290703125, 1024
px, py, zoom = -1.038650e-1, -9.584393e-1, 1.674667e+5
px, py, zoom = -0.761574, -0.0847596, 78125
px, py, zoom = -1.62917,-0.0203968, 3125
px, py, zoom = -0.75,0,1
R = 3 
max_iteration = 512
w, h = 1250,1250
mfactor = 1

def Mandelbrot(x,y,max_iteration,minx,maxx,miny,maxy):
    zx = 0
    zy = 0
    RX1, RX2, RY1, RY2 = px-R/2, px+R/2,py-R/2,py+R/2
    cx = (x-minx)/(maxx-minx)*(RX2-RX1)+RX1
    cy = (y-miny)/(maxy-miny)*(RY2-RY1)+RY1
    i=0
    while zx**2 + zy**2 <= 4 and i < max_iteration:
        temp = zx**2 - zy**2
        zy = 2*zx*zy + cy
        zx = temp + cx
        i += 1
    return i

def gen_Mandelbrot_image():
  bitmap = Image.new("RGB", (w, h), "white")
  pix = bitmap.load()
  for x in range(w):
    for y in range(h):
      c=Mandelbrot(x,y,max_iteration,0,w-1,0,h-1)
      v = c**mfactor/max_iteration**mfactor
      hv = 0.67-v*2
      #if hv<0: hv+=1
      r,g,b = colorsys.hsv_to_rgb(hv,1,1-(v-0.1)**2/0.9**2)
      r = min(255,round(r*255))
      g = min(255,round(g*255))
      b = min(255,round(b*255))
      pix[x,y] = int(r) + (int(g) << 8) + (int(b) << 16)
  bitmap.save("Mandelbrot_"+str(px)+"_"+str(py)+"_"+str(zoom)+".jpg")
  bitmap.show()
  
R=3/zoom
gen_Mandelbrot_image()

Zooming 10^13 into Mandelbrot Set Animation with Python (Source Code)

Write a Python program to generate 1000+ images of Mandelbrot Set with ever zooming ranges. Please note that Turtle is not used in this project for speed consideration. Then use an external software to combine the images into a video:

Zooming 10^13 into Mandelbrot Set

Source Code:

from PIL import Image
import colorsys
import math

px, py = -0.7746806106269039, -0.1374168856037867 #Tante Renate
R = 3 
max_iteration = 2500
w, h = 1024,1024
mfactor = 0.5

def Mandelbrot(x,y,max_iteration,minx,maxx,miny,maxy):
    zx = 0
    zy = 0
    RX1, RX2, RY1, RY2 = px-R/2, px+R/2,py-R/2,py+R/2
    cx = (x-minx)/(maxx-minx)*(RX2-RX1)+RX1
    cy = (y-miny)/(maxy-miny)*(RY2-RY1)+RY1
    i=0
    while zx**2 + zy**2 <= 4 and i < max_iteration:
        temp = zx**2 - zy**2
        zy = 2*zx*zy + cy
        zx = temp + cx
        i += 1
    return i

def gen_Mandelbrot_image(sequence):
  bitmap = Image.new("RGB", (w, h), "white")
  pix = bitmap.load()
  for x in range(w):
    for y in range(h):
      c=Mandelbrot(x,y,max_iteration,0,w-1,0,h-1)
      v = c**mfactor/max_iteration**mfactor
      hv = 0.67-v
      if hv<0: hv+=1
      r,g,b = colorsys.hsv_to_rgb(hv,1,1-(v-0.1)**2/0.9**2)
      r = min(255,round(r*255))
      g = min(255,round(g*255))
      b = min(255,round(b*255))
      pix[x,y] = int(r) + (int(g) << 8) + (int(b) << 16)
  bitmap.save("Mandelbrot_"+str(sequence)+".jpg")

R=3
f = 0.975
RZF = 1/1000000000000
k=1
while R>RZF:
  if k>100: break
  mfactor = 0.5 + (1/1000000000000)**0.1/R**0.1
  print(k,mfactor)
  gen_Mandelbrot_image(k)
  R *= f
  k+=1

This program generates over 1000 images and may be quite slow. You can make it faster by dividing the work to multiple programs each doing a portion of those images.

Zooming 10^13 Times into Julia Set Animation

Write a Python program to generate hundreds of images of Julia Set with ever zooming ranges. Please note that Turtle is not used in this project for speed consideration. Then use an external software to combine the images into a video:

Zooming 10^13 Times into Julia Set

The video above zooms into the center at 0.0958598997051 + 1.1501990019993i with c = 0.355 + 0.355i. In each iteration the range shrinks by 5% until the range becomes 1/10^13 the size of the original range. You can modify the code to try different centers and c. The following is the source code:

from PIL import Image
import colorsys

cx = -0.7269
cy = 0.1889
R = (1+(1+4*(cx**2+cy**2)**0.5)**0.5)/2+0.001
max_iteration = 512
print(R)

w, h, zoom = 1500,1500,1

def julia(cx,cy,RZ,px,py,R,max_iteration,x,y,minx,maxx,miny,maxy):
    zx = (x-minx)/(maxx-minx)*2*RZ-RZ+px
    zy = (y-miny)/(maxy-miny)*2*RZ-RZ+py
    i=0
    while zx**2 + zy**2 < R**2 and i < max_iteration:
        temp = zx**2 - zy**2
        zy = 2*zx*zy + cy
        zx = temp + cx
        i += 1
    return i

def gen_julia_image(sequence,cx,cy,RZ):
  bitmap = Image.new("RGB", (w, h), "white")
  pix = bitmap.load()
  for x in range(w):
    for y in range(h):
        # smllaer: right,down
      c=julia(cx,cy,RZ,0.0958598997051,1.1501990019993,R,max_iteration,x,y,0,w-1,0,h-1)
      v = c/max_iteration
      hv = 0.67-v*0.67
      r,g,b = colorsys.hsv_to_rgb(hv,1,v/2+0.45)
      r = min(255,round(r*255))
      g = min(255,round(g*255))
      b = min(255,round(b*255))
      pix[x,y] = int(r) + (int(g) << 8) + (int(b) << 16)
  bitmap.save("julia_"+str(sequence)+".jpg")
#  bitmap.show()

f = 0.95
RZF = 1/1000000000000
RZ = 1
k=1
while RZ>RZF:
  print(k,RZ)
  gen_julia_image(k,0.355,0.355,RZ)
  RZ *= f
  k+=1

Related Projects:

Julia Set Fractal Animation

Julia Set with Different Parameters

Julia Set with Different Parameters

In this project, we are generate several Julia Sets by varying the constant c in the normal Julia Set function. Turtle will be too slow to do this work. Instead we are going to use the PIL library. Please check out Julia Set Animation project for source code.

c = -0.618 (Max Iteration=38)
c = 0.355534-0.337292i (Max Iteration=4064)
c = 0.355+0.355i (Max Iteration=512)
c = 0.285+0.01i (Max Iteration=204)
c = -0.8+0.156i (Max Iteration=924)
c = -0.835+0.2321i (Max Iteration=104)
c = 0.7269+0.1889i (Max Iteration=1024)

Related Projects:

Zooming 10^13 Times into Julia Set Animation

Julia Set Fractal Animation

Julia Set Fractal Animation

Julia Set Animation

In this project, we are going to animate the Julia Set by varying the constant c in the normal Julia Set function. Turtle will be too slow to do this work. Instead we are going to use the PIL library to generate 360 images. Then, a video editing software can be used to combine 360 images into a video file. The following is the source code:

from PIL import Image
import colorsys
import math

cx = 0.7885
cy = 0
R = (1+(1+4*(cx**2+cy**2)**0.5)**0.5)/2+0.001
max_iteration = 200

w, h, zoom = 1000,1000,1

def julia(cx,cy,R,max_iteration,x,y,minx,maxx,miny,maxy):
    zx = (x-minx)/(maxx-minx)*2*R-R
    zy = (y-miny)/(maxy-miny)*2*R-R
    i=0
    while zx**2 + zy**2 < R**2 and i < max_iteration:
        temp = zx**2 - zy**2
        zy = 2*zx*zy + cy
        zx = temp + cx
        i += 1
    return i

def gen_julia_image(sequence,cx,cy):
  bitmap = Image.new("RGB", (w, h), "white")
  pix = bitmap.load()
  for x in range(w):
    for y in range(h):
      c=julia(cx,cy,R,max_iteration,x,y,0,w-1,0,h-1)
      r,g,b = colorsys.hsv_to_rgb(c/max_iteration,1,0.9)
      r = min(255,round(r*255))
      g = min(255,round(g*255))
      b = min(255,round(b*255))
      pix[x,y] = int(b) + (int(g) << 8) + (int(r) << 16)
  bitmap.save("julia_"+str(sequence)+".jpg")


for i in range(360):
  cx = 0.7885*math.cos(i*math.pi/180)
  cy = 0.7885*math.sin(i*math.pi/180)
  gen_julia_image(i,cx,cy)

Related Projects:

Julia Set with Different Parameters

Zooming 10^13 Times into Julia Set Animation

Colored Pentaflake Fractal (Source Code)

Based on petaflake fractal, color it with colorsys library.

Colored Pentaflake Fractal

Source Code:

import turtle
import math
import colorsys

screen = turtle.Screen()
screen.title('Pentaflake Fractal Colored - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)
turtle.fillcolor('dark cyan')

def pentagon(x,y,r,direction): #x,y is the center
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(direction)
    turtle.fd(r)
    turtle.left(126)
    turtle.down()
    c = colorsys.hsv_to_rgb((x+1000)/2000,1,0.7)
    turtle.color(c)
    turtle.begin_fill()
    for _ in range(5):
        turtle.fd(2*r*math.sin(math.radians(36)))
        turtle.left(72)
    turtle.end_fill()
    
def pentaflake(x,y,r,direction,n):
    if n==0:
        pentagon(x,y,r,direction)
        return

    r2 = r/(1+2*math.cos(math.radians(36)))
    d = 2*r2*math.cos(math.radians(36))
    for _ in range(5):
        x2,y2 = x+d*math.cos(math.radians(direction)),y+d*math.sin(math.radians(direction))
        pentaflake(x2,y2,r2,direction,n-1)
        direction += 72
    pentaflake(x,y,r2,direction+180,n-1)
    
pentaflake(0,0,1000,90,6)
screen.update()

Colored Spiral of Spirals with Python Turtle (Source Code)

Just for fun, draw a colored version of spiral of spirals using the colorsys library.

Colored Spiral of Spirals
Colored Spiral of Spirals

Source Code: (This may a few minutes)

import turtle
import colorsys

screen = turtle.Screen()
screen.title('Colored Spiral of Spirals Fractal - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)

def draw_spiral(x,y,length,direction):
    L = length
    c = 0
    color=colorsys.hsv_to_rgb((y+900)/1800,1,0.8)
    turtle.color(color)
    while length>1 or c<20:
        if length>2:
            draw_spiral(x,y,length*0.255,160+direction)
        turtle.up()
        turtle.seth(direction)
        turtle.goto(x,y)
        if length <= 2:
            turtle.down()
        turtle.fd(length)
        x,y = turtle.xcor(), turtle.ycor()
        length *= 0.93
        direction += 20
        c += 1
LL=300
draw_spiral(500,-500,LL,90)
screen.update()
ts=turtle.getscreen()
ts.getcanvas().postscript(file = "spiral.eps")