# Revision history [back]

# Adapted from http://preshing.com/20110831/penrose-tiling-explained

golden_ratio = (1 + sqrt(5))/2

def subdivide(triangles):
result = []
for color, A, B, C in triangles:
if color == 0:
# Subdivide red triangle
P = A + (B - A) / golden_ratio
result += [(0, C, P, B), (1, P, C, A)]
else:
# Subdivide blue triangle
Q = B + (A - B) / golden_ratio
R = B + (C - B) / golden_ratio
result += [(1, R, C, A), (1, Q, R, B), (0, R, Q, A)]
return result

# Create wheel of red triangles around the origin
triangles = []
for i in xrange(10):
B = exp((2*i - 1)*pi/10*I)
C = exp((2*i + 1)*pi/10*I)
if i % 2 == 0:
B, C = C, B  # Make sure to mirror every second triangle
triangles.append((0, 0, B, C))

# Draw n iterates
n = 5
for i in range(n):
P = Graphics()
for verts in triangles:
#P += polygon([(z.real(), z.imag()) for z in verts[1:]], color=['red', 'blue'][verts[0]])
P += line([(z.real(), z.imag()) for z in (verts[3], verts[1], verts[2])], color='black', thickness=2)
P.show(axes=False, aspect_ratio=1)
triangles = subdivide(triangles)