Binary Matrix Fractals using recursion. It can convert any given binary matrix pattern into a fractal.
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 | # Binary matrix fractals using recursion
# FB - 201003265
from PIL import Image
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]]
### Vicsek fractal
##bm = [[1,0,1], \
## [0,1,0], \
## [1,0,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]]
nx = len(bm[0])
ny = len(bm)
def bmf(x0, y0, x1, y1):
global image, bm, nx, ny
xd = x1-x0
yd = y1-y0
if xd < 2 and yd < 2:
image.putpixel((int(x0), int(y0)), 255)
return
for i in range(ny):
for k in range(nx):
if bm[i][k] > 0:
bmf(x0+xd*k/nx, y0+yd*i/ny, x0+xd*(k+1)/nx, y0+yd*(i+1)/ny)
# main
bmf(0, 0, imgx-1, imgy-1)
image.save("binMatFrR.png", "PNG")
|