The above image shows a famous woodcut by M.C. Escher called Square Limit composed of tesselating fish tiles. In this notebook, we will recreate this pattern using the HoloViews Spline element.

The construction used here is that of Peter Henderson's Functional Geometry paper and this notebook was inspired by Massimo Santini's programming-with-escher notebook, itself inspired by Haskell and Julia implementations.

We start by importing HoloViews and NumPy and loading the extension:

In [1]:
import holoviews as hv
import numpy as np

This notebook makes extensive use of the Spline element and we will want to keep equal aspects and suppress the axes:

In [2]:
%opts Spline [xaxis=None yaxis=None aspect='equal' bgcolor='white'] (linewidth=0.8)

'Square Limit' is composed from the following fish pattern, over which we show the unit square:

In [3]:

unitsquare = hv.Bounds((0,0,1,1))
fish = hv.Spline((spline, [1,4,4,4]*34)) # Cubic splines
fish * unitsquare

As you may expect, we will be applying a number of different geometric transforms to generate 'Square Limit'. To do this we will use Affine2D from matplotlib.transforms and matplotlib.path.Path (not to be confused with hv.Path !).

In [4]:
from matplotlib.path import Path
from matplotlib.transforms import Affine2D

# Define some Affine2D transforms
rotT = Affine2D().rotate_deg(90).translate(1, 0)
rot45T = Affine2D().rotate_deg(45).scale(1. / np.sqrt(2.), 1. / np.sqrt(2.)).translate(1 / 2., 1 / 2.)
flipT = Affine2D().scale(-1, 1).translate(1, 0)

def combine(obj):
    "Collapses overlays of Splines to allow transforms of compositions"
    if not isinstance(obj, hv.Overlay): return obj
    return hv.Spline((np.vstack([el.data[0] for el in obj.values()]),
                      np.hstack([el.data[1] for el in obj.values()])))
def T(spline, transform):
    "Apply a transform to a spline or overlay of splines"
    spline = combine(spline)        
    result = Path(spline.data[0], codes=spline.data[1]).transformed(transform)
    return hv.Spline((result.vertices, result.codes))

# Some simple transform functions we will be using
def rot(el):        return T(el,rotT)
def rot45(el):      return T(el, rot45T)
def flip(el):       return T(el, flipT)

Here we define three Affine2D transforms ( rotT , rot45T and flipT ), a function to collapse HoloViews Spline overlays (built with the * operator) in a single Spline element, a generic transform function T and the three convenience functions we will be using directly ( rot , rot45 and flip ). Respectively, these functions rotate the spline by $90^o$, rotate the spline by $45^o$ and flip the spline horizontally.

Here is a simple example of a possible tesselation:

In [5]:
fish * rot(rot(fish))

Next we need two functions, beside and above to place splines next to each other or one above the other, while compressing appropriately along the relevant axis:

In [6]:
def beside(spline1, spline2, n=1, m=1):
    den = n + m
    t1 = Affine2D().scale(n / den, 1)
    t2 = Affine2D().scale(m / den, 1).translate(n / den, 0)
    return combine(T(spline1, t1) * T(spline2, t2))

def above(spline1, spline2, n=1, m=1):
    den = n + m
    t1 = Affine2D().scale(1, n / den).translate(0, m / den)
    t2 = Affine2D().scale(1, m / den)
    return combine(T(spline1, t1) * T(spline2, t2))

beside(fish, fish)* unitsquare + above(fish,fish) * unitsquare

One import tile in 'Square Limit' is what we will call smallfish which is our fish rotate by $45^o$ then flipped:

In [7]:
smallfish = flip(rot45(fish))
smallfish * unitsquare

We can now build the two central tesselations that are necessary to build 'Square Limit' which we will call t and u respectively:

In [8]:
t =  fish *  smallfish * rot(rot(rot(smallfish)))
u = smallfish * rot(smallfish) * rot(rot(smallfish)) * rot(rot(rot(smallfish)))
t *unitsquare + u * unitsquare

We are now ready to define the two recursive functions that build the sides and corners of 'Square Limit' respectively. These recursive functions make use of quartet which is used to compress four splines into a small 2x2 grid:

In [9]:
blank = hv.Spline(([(np.nan, np.nan)],[1])) # An empty Spline object useful for recursion

def quartet(p, q, r, s):
    return above(beside(p, q), beside(r, s))

def side(n):
    if n == 0: 
        return hv.Spline(([(np.nan, np.nan)],[1]))
        return quartet(side(n-1), side(n-1), rot(t), t)
def corner(n):
    if n == 0:
        return hv.Spline(([(np.nan, np.nan)],[1]))
        return quartet(corner(n-1), side(n-1), rot(side(n-1)), u)

corner(2) + side(2)

We now have a way of building the corners and sides of 'Square Limit'. To do so, we will need one last function that will let us put the four corners and four sides in place together with the central tile:

In [10]:
def nonet(p, q, r, s, t, u, v, w, x):
    return above(beside(p, beside(q, r), 1, 2),
                 above(beside(s, beside(t, u), 1, 2),
                       beside(v, beside(w, x), 1, 2)), 1, 2)

args = [fish]* 4 + [blank] + [fish] * 4

Here we see use nonet to place eight of our fish around the edge of the square with a blank in the middle. We can finally use nonet together with our recursive corner and side functions to recreate 'Square Limit':

In [11]:
%%output size=250
def squarelimit(n):
    return nonet(corner(n), side(n), rot(rot(rot(corner(n)))),
                 rot(side(n)), u, rot(rot(rot(side(n)))), 
                 rot(corner(n)), rot(rot(side(n))), rot(rot(corner(n))))