Welcome, guest | Sign In | My Account | Store | Cart

It creates a fractal from any given binary matrix. It can re-create many of famous fractals.

Python, 58 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Binary Matrix IFS Fractals
# FB - 201003173
from PIL import Image
import random
# image size
imgx = 512
imgy = 512
image = Image.new("L", (imgx, imgy))

### Sierpinski triangle
##bm = [[1,0], \
##      [1,1]]

### Sierpinski square
##bm = [[1,1,1], \
##      [1,0,1], \
##      [1,1,1]]

### Snowflake
##bm = [[1,1,0], \
##      [1,0,1], \
##      [0,1,1]]

# Hexaflake
bm = [[1,1,0], \
      [1,1,1], \
      [0,1,1]]

### A spiral fractal
##bm = [[0,0,1,1,0], \
##      [1,0,1,0,0], \
##      [1,1,1,1,1], \
##      [0,0,1,0,1], \
##      [0,1,1,0,0]]

### Another spiral fractal
##bm = [[1,0,0,1,1], \
##      [1,0,1,0,0], \
##      [0,1,1,1,0], \
##      [0,0,1,0,1], \
##      [1,1,0,0,1]]

# size of the matrix
n = len(bm)
m = len(bm[0])

x = 0.0
y = 0.0

for i in range(imgx * imgy):
    j = random.randint(0, m - 1)
    k = random.randint(0, n - 1)
    if bm[k][j] > 0:
        x = (x + j) / m
        y = (y + k) / n
        image.putpixel((int(x * (imgx - 1)), int(y * (imgy - 1))), 255)

image.save("binMatIFSfr.png", "PNG")