Affine Transform Question

I set up these:

T1 = AffineTransform[{{{1/3, 0}, {0, 1/3}}, {0, 0}}];
T2 = AffineTransform[{{{1/3, 0}, {0, 1/3}}, {1/3, 0}}];
T3 = AffineTransform[{{{1/3, 0}, {0, 1/3}}, {2/3, 0}}];
T4 = AffineTransform[{{{1/3, 0}, {0, 1/3}}, {0, 1/3}}];
T5 = AffineTransform[{{{1/3, 0}, {0, 1/3}}, {2/3, 1/3}}];
T6 = AffineTransform[{{{1/3, 0}, {0, 1/3}}, {0, 2/3}}];
T7 = AffineTransform[{{{1/3, 0}, {0, 1/3}}, {1/3, 2/3}}];
T8 = AffineTransform[{{{1/3, 0}, {0, 1/3}}, {2/3, 2/3}}];


Then I did this:

g1 = Graphics[{
Rectangle[],
Red, GeometricTransformation[
Rectangle[], {T1, T2, T3, T4, T5, T6, T7, T8}]
}]


Which worked nicely. My next thought was to apply the transformations to g1.

g2 = Graphics[{
Rectangle[],
Red, GeometricTransformation[g1, {T1, T2, T3, T4, T5, T6, T7, T8}]
}]


But I got an error "Graphics is not a Graphics primitive or directive." What am I not understanding?

Thanks.

g2 just produces the same image g1 again

The new rectangles in g2 are not visible because they are all red. Change Red to a random color and they become visible:

g2 = Graphics[{Rectangle[], Hue[RandomReal[]],
GeometricTransformation[g1[[1]], {T1, T2, T3, T4, T5, T6, T7, T8}]}]


What I want to do is apply the eight transformations to g1 again, to produce the second step of a Sierpinski Carpet.

g1 /. r : Rectangle[_] :> {Hue[RandomReal[]],
GeometricTransformation[r, {T1, T2, T3, T4, T5, T6, T7, T8}]}


Grid[Partition[Nest[# /. r : Rectangle[_] :>
{Hue[RandomReal[]], GeometricTransformation[r, {T1, T2, T3, T4, T5, T6, T7, T8}]} &,
g1, #] & /@ {1, 2, 3, 4}, 2]]


Original post:

I got an error "Graphics is not a Graphics primitive or directive."

The first argument of GeometricTransformation should be a graphics primitive (such as Disk, Rectangle etc.). So GeometricTransformation[g1[[1]], {T1, T2, T3, T4, T5, T6, T7, T8}] works without complaint.

g2 = Graphics[{Rectangle[], Red,
GeometricTransformation[g1[[1]], {T1, T2, T3, T4, T5, T6, T7, T8}]}]


• Sorry, this just produces the same image g1 again. What I want to do is apply the eight transformations to g1 again, to produce the second step of a Sierpinski Carpet. But thanks for the help. – David May 15 '16 at 18:29
• @David, please see the update. – kglr May 15 '16 at 20:01
• Absolutely awesome response! Thanks, this is so helpful. – David May 15 '16 at 20:45
• @David, great question. Thank you for the accept. – kglr May 15 '16 at 20:53