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

It draws a different spiral IFS fractal each time.

Python, 31 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``` ```# Spiral IFS Fractals # FB36 - 20130914 from PIL import Image import math import random imgx = 1024; imgy = 1024 image = Image.new("RGB", (imgx, imgy)) pixels = image.load() n = random.randint(2, 9) # number of spiral arms m = random.randint(5, 12) # number of spirals in each arm a = 2.0 * math.pi / n # angle between arms b = 2.0 * math.pi * random.random() # max rotation (bending) angle for each arm rmax = 0.1 * random.random() + 0.1 # max spiral radius on each arm x = 0.0; y = 0.0 i = 0; p = 0 while True: k = random.randint(0, n - 1) # select an arm j = random.randint(0, m - 1) # select an spiral on the arm c = k * a + b * (j + 1.0) / m # angle of the spiral in the arm d = (j + 1.0) / m # distance of the spiral to the center r = d * rmax # radius of the spiral in the arm x = x * r + math.sin(c) * d y = y * r + math.cos(c) * d kx = int((x + 1.5) / 3.0 * (imgx - 1)) ky = int((y + 1.5) / 3.0 * (imgy - 1)) if pixels[kx, ky] == (0, 0, 0): pixels[kx, ky] = (255, 255, 255) p += 1 i += 1 if 100 * p / i < 20: break image.save("SpiralIFSFractal_" + str(n) + "_" + str(m) + ".png", "PNG") ```

#### 1 comment FB36 (author) 11 years, 8 months ago

Another version that has a spiral in the center:

``````# Spiral IFS Fractals
# FB - 201003162
from PIL import Image
import math
import random
# image size
imgx = 512
imgy = 512
image = Image.new("L", (imgx, imgy))
n = random.randint(2, 9)  # number of spiral arms
m = random.randint(5, 12) # number of spirals in each arm
a = 2.0 * math.pi / n     # angle between arms
b = 2.0 * math.pi*random.random() # max rotation (bending) angle for each arm
rmax = 0.2 * random.random() + 0.1 # max spiral radius on each arm
x = 0.0
y = 0.0

for i in range(imgx * imgy):
k = random.randint(0, n - 1) # select an arm
j = random.randint(0, m) # select an spiral on the arm
c = k * a + b * j * 1.0 / m # angle of the spiral in the arm
d = j * 1.0 / m # distance of the spiral to the center
if j > 0:
r = d * rmax # radius of the spiral in the arm
else:
r = rmax # radius of the spiral at the center
x = x * r + math.sin(c) * d
y = y * r + math.cos(c) * d
kx = int((x + 1.5) / 3.0 * (imgx - 1))
ky = int((y + 1.5) / 3.0 * (imgy - 1))
image.putpixel((kx, ky), 255)

image.save("spiralFr_.png", "PNG")
`````` Created by FB36 on Mon, 15 Mar 2010 (MIT)