In [86]:
def cantor_dust(center_x = 0, center_y = 0, sides=5, size=150, contraction=0.2, steps=2):
    '''
    Computes cantor's dust of the given size. Note that Cantor's dust is a non-countable fractal, so that this an
    approximation after steps iterations.

    Arguments:
        @center_x, @center_y: Center coordinates
        @sides: Number of sides. cantor_dust generates a fractal by contracting a regular polygon with "sides" number of sides.
        @size: Size of the first polygon from center to vertex.
        @contraction: Contraction factor applied during iteration
        @steps: Number of steps to iterate. Note that steps=0 means no iteration at all (Only keep the first polygons' vertices).

    Returns:
        An iterator for the coordinates (x, y) of the vertices, generated recursively on each contraction.
    '''
    if steps == 0:
        for i in range(sides):
            yield (center_x + size * sin(2 * pi * i/ sides), center_y + size * cos(2 * pi * i/ sides))
    else:
        for i in range(sides):
            new_center_x = center_x + (1 - contraction) * size * sin(2 * pi * i / sides)
            new_center_y = center_y + (1 - contraction) * size * cos(2 * pi * i / sides)
            for x, y in cantor_dust(new_center_x, new_center_y, sides, size * contraction, contraction, steps - 1):
                yield (x, y)
In [131]:
points = list(cantor_dust(contraction=0.3, steps=4))
x = [p[0] for p in points]
y = [p[1] for p in points]
plot(x, y, ',')
Out[131]:
[<matplotlib.lines.Line2D at 0xafff27cc>]
In [ ]: