HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
Deutsch   English   

15. Fractals

 

 

YOU LEARN HERE...

 

to orientate yourself in more complex programme codes and make certain adjustments. The formulae used in the following examples were invented by well-known mathematicians.

 

 

EXAMPLES

 

Fractals are computer-generated images that are self-similar, i.e. in which partial images are a reduced copy of the overall image. The term fractal was introduced in 1975 by mathematician Benoit Mandelbrot and comes from the Latin fractus (broken), which refers to the so-called fractal dimension. In traditional geometry, a line is one-dimensional, a surface is two-dimensional and a space is three-dimensional. Fractal images usually have a non-integer dimension. Fractals created by a non-linear transformation provide interesting images. Such fractals are usually calculated iteratively and often use complex numbers.

Below you will find the complete programme code for some well-known fractals without further comment. Even without studying the program code in detail, you can enjoy the beautiful images and create fractals with different colours.

Example 1: Fern

Program:     

# Farn.py

from gpanel import *
import random

def farn():
    z = 0
    n = 0
    while n < nbPoints:
        r = random.random()
        c = "black"
        if r < 0.01:
            c = "yellow"
            z = f(z, 0, 0, 0, 0.16, 0, 0) # Stiel
        elif r < 0.86:
            c = "lime"
            z = f(z, 0.85, 0.04, -0.04, 0.85, 0, 1.60) # symmetry
        elif r > 0.86 and r < 0.93:
            c = "red"
            z = f(z, 0.20, -0.26, 0.23, 0.22, 0, 1.60)  # left leaves
        elif r > 0.93:
            c = "blue"
            z = f(z, -0.15, 0.28, 0.26, 0.24, 0, 1.44) # right leaves
        setColor(c)
        point(z)
        n += 1

def f(z, a, b, c, d, e, f):
    re = a * z.real + b * z.imag + e
    im = c * z.real + d * z.imag + f
    return complex(re, im)

makeGPanel(-3.5, 3.5, 0, 10)
bgColor("black")
nbPoints = 40000
farn()
► Copy to clipboard


Example 2
: Julia

Program:      

# Julia.py

from gpanel import *

def putPixel(z, c):
  setColor(c)
  point(z.real, z.imag)

maxIter = 100
maxNorm = 50.0
step = 0.005
range = 2.0
makeGPanel(-range, range, -range, range)
c = complex(-0.5, -0.5)
z0 = complex(-range, -range)

while z0.imag < range: # outer loop in imag direction
    z0 = complex(-range, z0.imag + step)
    while z0.real < range: # inner loop in real direction
        z0 = z0 + step
        z = z0;
        it = 0
        while (z.real * z.real + z.imag * z.imag) < maxNorm and it < maxIter:
            z = z * z + c
            it = it + 1
        if it < 3:
            putPixel(z0, "darkblue")
        elif it < 5:
            putPixel(z0, "lime")
        elif it < 8:
            putPixel(z0, "red")
        elif it < 12:
            putPixel(z0, "blue")
        elif it < 100:
             putPixel(z0, "yellow")
        else:
            putPixel(z0, "black")
► Copy to clipboard

 

Example 3: Mandelbrot


Program:      

#Mandelbrot.py

from gpanel import *
    
def mandelbrot(c):
    z = 0
    for it in range(maxIterations):
        z = z*z + c
        if abs(z) > R: # diverging
            return it
    return maxIterations

maxIterations = 50
R = 2
xmin = -2
xmax = 1
xstep = 0.003
ymin = -1.5
ymax = 1.5
ystep = 0.003

makeGPanel(xmin, xmax, ymin, ymax)
y = ymin
while y <= ymax:
    x = xmin
    while x <= xmax:
        c = x + y*1j
        itCount = mandelbrot(c)
        if itCount == maxIterations: # inside Mandelbrot set
            setColor("yellow")
        else: # outside Mandelbrot set
           setColor((30 * itCount) % 256, 
                      (4 * itCount) % 256, 
                      (255 - (30 * itCount)) % 256)
        point(c)
        x += xstep
    y += ystep
► Copy to clipboard

 

 

TO SOLVE BY YOURSELF

 

1)


Draw Mandelbrot fractals in different colours.