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[4] /. Line -> BSplineCurve}]

2 Answers 2

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], 


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

Using first the example in OP:

cmesh = CantorMesh[2, 2];

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

enter image description here

smesh = SierpinskiMesh[3];

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

enter image description here

pts = RandomReal[1, {12, 2}];
vmesh = VoronoiMesh[pts];

Row[Show[#, ImageSize -> Small] & /@ 
  Flatten[{vmesh, replacePolygons[vmesh, #] & /@ {shape1, shape2}}], Spacer[15]]

enter image description here


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 /. Polygon[pts_] :>
          Disk[Mean[pts], Apply[EuclideanDistance, Take[pts, 2]]/2]]} // GraphicsRow

2D Cantor mesh, square and disk


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.