I am trying to draw a Sierpinski_carpet. I have code that works, but I think there is a more elegant way to do than my way. Maybe I couls use Tuples or Permutations or some similar function to simplify my code.

f[{{x1_, y1_}, {x2_, y2_}}] := Map[Mean, {
{{{x1, x1, x1}, {y1, y1, y1}}, {{x1, x1, x2}, {y1, y1, y2}}},
{{{x1, x1, x1}, {y1, y1, y2}}, {{x1, x1, x2}, {y1, y2, y2}}},
{{{x1, x1, x1}, {y1, y2, y2}}, {{x1, x1, x2}, {y2, y2, y2}}},
{{{x1, x1, x2}, {y1, y1, y1}}, {{x1, x2, x2}, {y1, y1, y2}}},
{{{x1, x1, x2}, {y1, y2, y2}}, {{x1, x2, x2}, {y2, y2, y2}}},
{{{x1, x2, x2}, {y1, y1, y1}}, {{x2, x2, x2}, {y1, y1, y2}}},
{{{x1, x2, x2}, {y1, y1, y2}}, {{x2, x2, x2}, {y1, y2, y2}}},
{{{x1, x2, x2}, {y1, y2, y2}}, {{x2, x2, x2}, {y2, y2, y2}}}
}, {3}];
d = Nest[Join @@ f /@ # &, {{{0., 0.}, {1, 1}}}, 3];
Graphics[Rectangle @@@ d]
Clear["`*"]

























PixelConstrained -> 1and for the beautiful images. – Federico Mar 25 '13 at 22:35carpet[n_] := Nest[ArrayFlatten[{{{#, #, #}, {#, 0, #}, {#, #, #}}, {{#, 0, #}, {0, 0, 0}, {#, 0, #}}, {{#, #, #}, {#, 0, #}, {#, #, #}}}, 3] &, 1, n]; Image3D[carpet[3]]– chyanog Mar 26 '13 at 03:35