English: Fibonacci word fractal, order 18, with the sub-rectangles colored
Drawn in python.
```python
from itertools import count, islice, groupby
from math import floor, sqrt
import matplotlib.pyplot as plt
def sturm_word(ratio):
xs = count(1)
ys = map(lambda x : x * ratio, xs)
fw = islice(groupby(map(floor, ys)), 1, None)
return map(lambda x : 2 - len(list(x[1])), fw)
- If the word is 1, then draw a line forward.
- If the word is 0, then first draw a line forward
# if the symbol 0 is in an even position then turn left, else turn right
def fib_line(fib_word, origin=(0, 0), length=1, angle=0):
angle_dict = {
0: (1, 0),
90: (0, 1),
180: (-1, 0),
270: (0, -1)
}
is_even = True
x0, y0 = origin
current_angle=angle
while True:
dx, dy = angle_dict[current_angle]
x1, y1 = x0 + dx, y0 + dy
yield (x0, y0, x1, y1)
if next(fib_word) == 0:
if is_even:
current_angle += 90
else:
current_angle -= 90
current_angle %= 360
x0, y0 = x1, y1
is_even = not is_even
golden_ratio = (sqrt(5) - 1)/2
fib_word = sturm_word(golden_ratio)
def fibs(n):
f0, f1 = 0, 1
for _ in range(n):
f0, f1 = f1, f0+f1
return f0
xfirst, yfirst, xlast, ylast = 0, 0, 0, 0
golden_ratio = (sqrt(5) - 1)/2
fib_word = sturm_word(golden_ratio)
i = 0
fib_i = fibs(i)
limit_i = 18
plt.figure (figsize= (20, 32))
for line_i, (x0, y0, x1, y1) in enumerate(fib_line(fib_word, origin=(0, 0), length=0.1, angle=0)):
plt.plot([x0, x1], [y0, y1], 'k')
if line_i > fib_i - 2:
i, fib_i = i+1, fibs(i+1)
xfirst, yfirst, xlast, ylast = xlast, ylast, x1, y1
color = 'blue' if i % 2 == 0 else 'green'
r = plt.Rectangle((xfirst, yfirst), xlast-xfirst, ylast-yfirst, fc=color, ec='black', alpha=0.3)
plt.gca().add_patch(r)
if i > limit_i:
break
plt.gca().set_aspect('equal')
plt.axis('off')
plt.savefig('fib_word.svg', bbox_inches=0, transparent=True)
```