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

Quasicrystal Pattern Generator

Python, 28 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
# Quasicrystal Pattern Generator
# https://en.wikipedia.org/wiki/Quasicrystal
# http://mainisusuallyafunction.blogspot.com/2011/10/quasicrystals-as-sums-of-waves-in-plane.html
# FB - 20150808
import math
import random
from PIL import Image
imgx = 512; imgy = 512
image = Image.new("RGB", (imgx, imgy))
pixels = image.load()

f = random.random() * 40 + 10 # frequency
p = random.random() * math.pi # phase
n = random.randint(10, 20) # of rotations
print f, p, n

for ky in range(imgy):
    y = float(ky) / (imgy - 1) * 4 * math.pi - 2 * math.pi
    for kx in range(imgx):
        x = float(kx) / (imgx - 1) * 4 * math.pi - 2 * math.pi
        z = 0.0
        for i in range(n):
            r = math.hypot(x, y)
            a = math.atan2(y, x) + i * math.pi * 2.0 / n
            z += math.cos(r * math.sin(a) * f + p)
        c = int(round(255 * z / n))
        pixels[kx, ky] = (c, c, c) # grayscale
image.save("quasicrystal.png", "PNG")

1 comment

ilevkivskyi 8 years, 6 months ago  # | flag

Cool generator! But I have one critical comment: You might be surprised but there are many people who use Python 3.x. I just added () to print and all worked fine. This simple step will make your code much more 2 and 3 compatible.

Thanks.