# Replacing every shape in a geometric set with another shape

Consider a Mathematica function that produces some number of identical, but separate shapes; like CantorMesh[2, 2], for example.

Is there a simple way to "replace" each of the identical shapes in the produced object with another compatible geometric region/shape, like circles?

This question is inspired by the nice Wolfram-provided example of replacing every line in a Hilbert curve with a spline:

Graphics[{Thickness[Large], HilbertCurve /. Line -> BSplineCurve}]


ClearAll[replacePolygons]
replacePolygons[msh_, shp_, s_: .5] := Module[{sc = -Subtract @@@ RegionBounds[msh],
mp = MeshPrimitives[msh, 2]},
Graphics[mp /. p_Polygon :> Translate[Scale[shp, -s (Subtract @@@ RegionBounds[p])/sc],
RegionCentroid[p]]]]


Examples:

shape1 = Disk[];
shape2 = Polygon[CirclePoints];


Using first the example in OP:

cmesh = CantorMesh[2, 2];

Row[Show[#, ImageSize -> Small] & /@
Flatten[{cmesh, replacePolygons[cmesh, #] & /@ {shape1, shape2}}], Spacer] smesh = SierpinskiMesh;

Row[Show[#, ImageSize -> Small] & /@
Flatten[{smesh, replacePolygons[smesh, #] & /@ {shape1, shape2}}], Spacer] SeedRandom
pts = RandomReal[1, {12, 2}];
vmesh = VoronoiMesh[pts];

Row[Show[#, ImageSize -> Small] & /@
Flatten[{vmesh, replacePolygons[vmesh, #] & /@ {shape1, shape2}}], Spacer] It really depends on what kind of substitutions you want to do. For something as simple as replacing a square with a disk, something like the following can be done:

polys = MeshPrimitives[CantorMesh[2, 2], 2];

{Graphics[polys],
Graphics[polys /. Polygon[pts_] :>
Disk[Mean[pts], Apply[EuclideanDistance, Take[pts, 2]]/2]]} // GraphicsRow 