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

Dragon IFS Fractal

Python, 45 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
# Dragon Fractal using IFS method
# FB - 20120106
import random
from PIL import Image
imgx = 512
imgy = 512
image = Image.new("RGB", (imgx, imgy))
maxIt = 100000 # max iterations allowed

n = 6 # coloring level
color = [0] * n
red = []
grn = []
blu = []
for j in range(2):
    red.append(random.randint(0, 255))
    grn.append(random.randint(0, 255))
    blu.append(random.randint(0, 255))

xa = -1.0 / 3
xb = 7.0 / 6
ya = -1.0 / 3
yb = 2.0 / 3

x = 0.0
y = 0.0
for i in range(maxIt):
    k = random.randint(0, 1)
    for m in range(n - 1):
        color[m] = color[m + 1]
    color[n - 1] = k
    if k == 0:
        x0 = (x - y) / 2.0
        y = (x + y) / 2.0
        x = x0
    else:
        x0 = 1.0 - (x + y) / 2.0
        y = (x - y) / 2.0
        x = x0
    kx = int((x - xa) / (xb - xa) * (imgx - 1))
    ky = int((y - ya) / (yb - ya) * (imgy - 1))
    if kx >=0 and kx < imgx and ky >= 0 and ky <= imgy:
        image.putpixel((kx, ky), (red[color[0]], grn[color[0]], blu[color[0]]))

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

2 comments

FB36 (author) 9 years, 11 months ago  # | flag

And this is the C Fractal using IFS method:

# C Fractal using IFS method
# FB - 20120106
import random
from PIL import Image
imgx = 512
imgy = 512
image = Image.new("RGB", (imgx, imgy))
maxIt = 100000 # max iterations allowed

n = 6 # coloring level
color = [0] * n
red = []
grn = []
blu = []
for j in range(2):
    red.append(random.randint(0, 255))
    grn.append(random.randint(0, 255))
    blu.append(random.randint(0, 255))

xa = -0.5
xb = 1.5
ya = -0.25
yb = 1.0

x = 0.0
y = 0.0
for i in range(maxIt):
    k = random.randint(0, 1)
    for m in range(n - 1):
        color[m] = color[m + 1]
    color[n - 1] = k
    if k == 0:
        x0 = (x - y) / 2.0
        y = (x + y) / 2.0
        x = x0
    else:
        x0 = (x + y) / 2.0 + 0.5
        y = (y - x) / 2.0 + 0.5
        x = x0
    kx = int((x - xa) / (xb - xa) * (imgx - 1))
    ky = int((y - ya) / (yb - ya) * (imgy - 1))
    if kx >=0 and kx < imgx and ky >= 0 and ky <= imgy:
        image.putpixel((kx, ky), (red[color[0]], grn[color[0]], blu[color[0]]))

image.save("CFractal_IFS.png", "PNG")
LL Snark 9 years, 11 months ago  # | flag

And The turtle C curve :

from turtle import *
from math import sqrt
sqrt2=sqrt(2)

def ccurve(l) :
    if l<1 :
        fd(l)
        return
    lt(45)
    ccurve(l/sqrt2)
    rt(90)
    ccurve(l/sqrt2)
    lt(45)

speed(0)
tracer(50,0)
ccurve(100)
update()