Q:

How to create a repetitive image pattern programmatically?

I want to create the following image patterns using Python.

For clarity: these are two separate image sequences (one on the top row, one on the bottom row).
They are related to each other as they are projected areas of stacked tetrahedra.
In a 3D environment it looks as follows:

Note that these 3D object are not scaled such that the total object dimension remains the same. This is the case with the projected areas shown above.
The four level structure (not shown) would have an additional 10 cells on top.
The total amount of cells C at level n is:
C = (n^3 + 3*n^2 + 2*n)/6 

Now I'm creating the patterns by hand (make 3D object, render out projected area, repeat) but this is very tedious and not feasible for more subdivisions.
I managed to create a single polygon with the following code, but I can't figure out how to loop this such that the total edge length stays the same, but the polygon gets subdivided in the way visualised above.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

el = 1
dfv = 1/np.sqrt(3)*el
dfe = 1/(2*np.sqrt(3))*el
vertices1 = [(0,0),(0.5*el,-dfe),(0,dfv),(-0.5*el,-dfe)]
vertices2 = [(0.5*el,-dfe),(0,dfv),(-0.5*el,-dfe)]

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.add_patch(Polygon(vertices1, closed=True, fill=True))
ax1.set_xlim([-1, 1])
ax1.set_ylim([-1, 1])
ax1.set_aspect('equal')

ax2 = fig.add_subplot(122)
ax2.add_patch(Polygon(vertices2, closed=True, fill=True))
ax2.set_xlim([-1, 1])
ax2.set_ylim([-1, 1])
ax2.set_aspect('equal')
plt.show()

I used matplotlib and the included Polygon patch, but I'm not sure if that is the most optimal method.
Also the orientation of the polygon or the color is of no importance.

A:

Helper class: 
class Custom_Polygon(object):
    """docstring for Polygon"""
    def __init__(self, verts):
        self.verticies = np.array(verts)
        self.dims = self.verticies.shape[1]

    def scale(self, scaleFactor):
        scaleMatrix = np.zeros((self.dims, self.dims))
        np.fill_diagonal(scaleMatrix, scaleFactor)
        self.verticies = np.dot(self.verticies, scaleMatrix)

    def scale_with_orgin(self, scaleFactor, origin):
        origin = origin.copy()
        self.translate([-i for i in origin])
        self.scale(scaleFactor)
        self.translate([i for i in origin])

    def translate(self, shiftBy):
        self.verticies += shiftBy

    def get_width(self):
        x_min = self.verticies[:,0].min()
        x_max = self.verticies[:,0].max()
        return abs(x_max - x_min)

    def get_height(self):
        y_min = self.verticies[:,1].min()
        y_max = self.verticies[:,1].max()
        return abs(y_max - y_min)

Made a helper class for scaling and translating the polygon around to make the pattern. And wrote the algorithm for drawing the first pattern. Should not be hard to make a similar algorithm for drawing the second pattern.    
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

def makeFirstPattern(ax, d, verticies, scale=True):
    Pn = Custom_Polygon(verticies)
    direction = 1 
    divisions = d
    if scale: Pn.scale_with_orgin(2.0/(divisions+1), Pn.verticies[-1])
    for i in range(divisions, 0, -2):
        for _ in range(i):
            ax.add_patch(Polygon(Pn.verticies, closed=True, fill=True, ec='none'))
            Pn.translate([direction * Pn.get_width()/2, 0])
        direction *= -1
        Pn.translate([direction * Pn.get_width(), Pn.get_height()])

el = 1
dfv = 1/np.sqrt(3)*el
dfe = 1/(2*np.sqrt(3))*el
vertices1 = [(0,0),(0.5*el,-dfe),(0,dfv),(-0.5*el,-dfe)]
vertices2 = [(0.5*el,-dfe),(0,dfv),(-0.5*el,-dfe)]

fig = plt.figure()
ax1 = fig.add_subplot(111)

makeFirstPattern(ax1, 7, vertices2)

ax1.set_xlim([-1, 1])
ax1.set_ylim([-1, 1])
ax1.set_aspect('equal')
plt.show()

