In [1]:
COMPLEMENT=120
FACTOR = math.sqrt(3)/2

class Carpet:
  def __init__(self, x, y, side):
    self.x = x
    self.y = y
    self.side = side
    self.height = FACTOR*side

  def _children(self, x, y, side, height, step=0):
    if step==0:
      yield (x, y)

    else:
      newSide = side/2
      newHeight = height/2

      for f in self._children(x, y, newSide, newHeight, step - 1):
        yield f
      for f in self._children(x + newSide, y, newSide, newHeight, step - 1):
        yield f
      for f in self._children(x + newSide/2, y + newHeight, newSide, newHeight, step - 1):
        yield f

  def children(self, steps=0):
    return self._children(self.x, self.y, self.side, self.height, steps)
In [39]:
c = Carpet(0, 0, 500)
In [40]:
#We are working recursively, and though python's iterators are very good in preserving memory, you should not
#push too hard on the steps, unless you wish to crash the notebook ;-)
points =  list(c.children(steps=7))
X = list(map(lambda z : z[0], points))
Y = list(map(lambda z : z[1], points))
In [41]:
plot(X, Y, 'r.', markersize=2)
Out[41]:
[<matplotlib.lines.Line2D at 0xb00c830c>]