Skip to main content
added 1 character in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
ClearAll[splitCoords, step, recursiveSubdivide]

splitCoords = Map[Append[#2]]@
  Partition[RotateRight @ 
    Riffle[#, MovingAverage[Join[#, {First@#}], 2]], 3, 2,1] &;

step = ReplaceAll[p_Polygon :> 
  Module[{rc = RegionCentroid[p], mc = MeshCoordinates[p]}, 
     Map[Polygon] @ splitCoords[mc, rc]]];


recursiveSubdivide[rp_, n_, opts : OptionsPattern[]] /; n >= 1 := 
 DynamicModule[{p = RegionCentroid[rp], mc = MeshCoordinates[rp]}, 
  Deploy @ Graphics[{EdgeForm[Gray], 
     Dynamic @ Map[{{Opacity[.5], FaceForm[RandomColor[]], #} & /@ 
        Nest[Flatten @* step, Polygon @ #, n]} &] @ splitCoords[mc, p], 
     Locator[Dynamic@p]}, opts]]

Examples:

SeedRandom[777];
rp = RandomPolygon[{"Convex", 4}];

recursiveSubdivide[rp, 1, ImageSize -> Medium]

enter image description here enter image description here

recursiveSubdivide[rp, 3, ImageSize -> Medium]

enter image description here

enter image description here

ClearAll[splitCoords, step, recursiveSubdivide]

splitCoords = Map[Append[#2]]@
  Partition[RotateRight @ 
    Riffle[#, MovingAverage[Join[#, {First@#}], 2]], 3, 2,1] &;

step = ReplaceAll[p_Polygon :> 
  Module[{rc = RegionCentroid[p], mc = MeshCoordinates[p]}, 
     Map[Polygon] @ splitCoords[mc, rc]]];


recursiveSubdivide[rp_, n_, opts : OptionsPattern[]] /; n >= 1 := 
 DynamicModule[{p = RegionCentroid[rp], mc = MeshCoordinates[rp]}, 
  Deploy @ Graphics[{EdgeForm[Gray], 
     Dynamic @ Map[{{Opacity[.5], FaceForm[RandomColor[]], #} & /@ 
        Nest[Flatten @* step, Polygon @ #, n]} &] @ splitCoords[mc, p], 
     Locator[Dynamic@p]}, opts]]

Examples:

SeedRandom[777];
rp = RandomPolygon[{"Convex", 4}];

recursiveSubdivide[rp, 1, ImageSize -> Medium]

enter image description here enter image description here

recursiveSubdivide[rp, 3 ImageSize -> Medium]

enter image description here

enter image description here

ClearAll[splitCoords, step, recursiveSubdivide]

splitCoords = Map[Append[#2]]@
  Partition[RotateRight @ 
    Riffle[#, MovingAverage[Join[#, {First@#}], 2]], 3, 2,1] &;

step = ReplaceAll[p_Polygon :> 
  Module[{rc = RegionCentroid[p], mc = MeshCoordinates[p]}, 
     Map[Polygon] @ splitCoords[mc, rc]]];


recursiveSubdivide[rp_, n_, opts : OptionsPattern[]] /; n >= 1 := 
 DynamicModule[{p = RegionCentroid[rp], mc = MeshCoordinates[rp]}, 
  Deploy @ Graphics[{EdgeForm[Gray], 
     Dynamic @ Map[{{Opacity[.5], FaceForm[RandomColor[]], #} & /@ 
        Nest[Flatten @* step, Polygon @ #, n]} &] @ splitCoords[mc, p], 
     Locator[Dynamic@p]}, opts]]

Examples:

SeedRandom[777];
rp = RandomPolygon[{"Convex", 4}];

recursiveSubdivide[rp, 1, ImageSize -> Medium]

enter image description here enter image description here

recursiveSubdivide[rp, 3, ImageSize -> Medium]

enter image description here

enter image description here

added 37 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
iterClearAll[splitCoords, =step, ReplaceAll[p_PolygonrecursiveSubdivide]

splitCoords :>= Map[Append[#2]]@
  Partition[RotateRight @ Module[{rc 
 = RegionCentroid[p], mc =Riffle[#, MeshCoordinates[p]}MovingAverage[Join[#, 
 {First@#}], 2]], 3, 2,1] &;

step Map[Polygon= @*ReplaceAll[p_Polygon Append[rc]]:> @
  Module[{rc = RegionCentroid[p], mc = Partition[RotateRightMeshCoordinates[p]}, @
      Map[Polygon] @ Riffle[mcsplitCoords[mc, MovingAverage[Append[First@#]@# &@mcrc]]];


recursiveSubdivide[rp_, 2]]n_, 3,opts 2,: 1]]];

foo[rp_,OptionsPattern[]] n_]/; n >= 1 := 
 DynamicModule[{p = RegionCentroid[rp], mc = MeshCoordinates[rp]}, 
  Deploy @ Graphics[{FaceForm[], EdgeForm[Gray], 
     Dynamic @ Map[{EdgeForm[Gray], 
           {Opacity[.5], FaceForm[RandomColor[]], #} & /@ 
          Nest[Flatten Nest[Flatten@*iter,@* Polygon@#step, n]} &]@
       Map[Append[p]]@ Partition[RotateRightPolygon @
          Riffle[mc#, MovingAverage[Append[First@#]@# &@mc,n]} 2]],&] 3,@ 2splitCoords[mc, 1]p], 
     Locator[Dynamic@p]}, ImageSize -> Large]]opts]]

Examples:

SeedRandom[777];
rp = RandomPolygon[{"Convex", 4}];

foo[rprecursiveSubdivide[rp, 1]1, ImageSize -> Medium]

enter image description here enter image description here

foo[rprecursiveSubdivide[rp, 3]3 ImageSize -> Medium]

enter image description here

enter image description here

iter = ReplaceAll[p_Polygon :> 
    Module[{rc = RegionCentroid[p], mc = MeshCoordinates[p]}, 
      Map[Polygon @* Append[rc]] @
       Partition[RotateRight @
        Riffle[mc, MovingAverage[Append[First@#]@# &@mc, 2]], 3, 2, 1]]];

foo[rp_, n_] := 
 DynamicModule[{p = RegionCentroid[rp], mc = MeshCoordinates[rp]}, 
  Deploy @ Graphics[{FaceForm[], EdgeForm[Gray], 
     Dynamic @ Map[{EdgeForm[Gray], 
           {Opacity[.5], FaceForm[RandomColor[]], #} & /@ 
           Nest[Flatten@*iter, Polygon@#, n]} &]@
       Map[Append[p]]@ Partition[RotateRight @
          Riffle[mc, MovingAverage[Append[First@#]@# &@mc, 2]], 3, 2, 1], 
     Locator[Dynamic@p]}, ImageSize -> Large]]

Examples:

SeedRandom[777];
rp = RandomPolygon[{"Convex", 4}];

foo[rp, 1]

enter image description here enter image description here

foo[rp, 3]

enter image description here

enter image description here

ClearAll[splitCoords, step, recursiveSubdivide]

splitCoords = Map[Append[#2]]@
  Partition[RotateRight @  
    Riffle[#, MovingAverage[Join[#, {First@#}], 2]], 3, 2,1] &;

step = ReplaceAll[p_Polygon :> 
  Module[{rc = RegionCentroid[p], mc = MeshCoordinates[p]}, 
     Map[Polygon] @ splitCoords[mc, rc]]];


recursiveSubdivide[rp_, n_, opts : OptionsPattern[]] /; n >= 1 := 
 DynamicModule[{p = RegionCentroid[rp], mc = MeshCoordinates[rp]}, 
  Deploy @ Graphics[{EdgeForm[Gray], 
     Dynamic @ Map[{{Opacity[.5], FaceForm[RandomColor[]], #} & /@ 
        Nest[Flatten @* step, Polygon @ #, n]} &] @ splitCoords[mc, p], 
     Locator[Dynamic@p]}, opts]]

Examples:

SeedRandom[777];
rp = RandomPolygon[{"Convex", 4}];

recursiveSubdivide[rp, 1, ImageSize -> Medium]

enter image description here enter image description here

recursiveSubdivide[rp, 3 ImageSize -> Medium]

enter image description here

enter image description here

deleted 4 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
iter = ReplaceAll[p_Polygon :> 
    Module[{rc = RegionCentroid[p], mc = MeshCoordinates[p]}, 
     Map[Polygon]Map[Polygon @@* Map[Append[rc]]Append[rc]] @
       Partition[RotateRight @
        Riffle[mc, MovingAverage[Append[First@#]@# &@mc, 2]], 3, 2, 1]]];

foo[rp_, n_] := 
 DynamicModule[{p = RegionCentroid[rp], mc = MeshCoordinates[rp]}, 
  Deploy @ Graphics[{FaceForm[], EdgeForm[Gray], 
     Dynamic @ Map[{EdgeForm[Gray], 
           {Opacity[.5], FaceForm[RandomColor[]], #} & /@ 
           Nest[Flatten@*iter, Polygon@#, n]} &]@
       Map[Append[p]]@ Partition[RotateRight @
          Riffle[mc, MovingAverage[Append[First@#]@# &@mc, 2]], 3, 2, 1], 
     Locator[Dynamic@p]}, ImageSize -> Large]]

Examples:

SeedRandom[777];
rp = RandomPolygon[{"Convex", 4}];

foo[rp, 1]

enter image description here enter image description here

foo[rp, 3]

enter image description here

enter image description here

iter = ReplaceAll[p_Polygon :> 
    Module[{rc = RegionCentroid[p], mc = MeshCoordinates[p]}, 
     Map[Polygon] @ Map[Append[rc]] @
       Partition[RotateRight @
        Riffle[mc, MovingAverage[Append[First@#]@# &@mc, 2]], 3, 2, 1]]];

foo[rp_, n_] := 
 DynamicModule[{p = RegionCentroid[rp], mc = MeshCoordinates[rp]}, 
  Deploy @ Graphics[{FaceForm[], EdgeForm[Gray], 
     Dynamic @ Map[{EdgeForm[Gray], 
           {Opacity[.5], FaceForm[RandomColor[]], #} & /@ 
           Nest[Flatten@*iter, Polygon@#, n]} &]@
       Map[Append[p]]@ Partition[RotateRight @
          Riffle[mc, MovingAverage[Append[First@#]@# &@mc, 2]], 3, 2, 1], 
     Locator[Dynamic@p]}, ImageSize -> Large]]

Examples:

SeedRandom[777];
rp = RandomPolygon[{"Convex", 4}];

foo[rp, 1]

enter image description here enter image description here

foo[rp, 3]

enter image description here

enter image description here

iter = ReplaceAll[p_Polygon :> 
    Module[{rc = RegionCentroid[p], mc = MeshCoordinates[p]}, 
     Map[Polygon @* Append[rc]] @
       Partition[RotateRight @
        Riffle[mc, MovingAverage[Append[First@#]@# &@mc, 2]], 3, 2, 1]]];

foo[rp_, n_] := 
 DynamicModule[{p = RegionCentroid[rp], mc = MeshCoordinates[rp]}, 
  Deploy @ Graphics[{FaceForm[], EdgeForm[Gray], 
     Dynamic @ Map[{EdgeForm[Gray], 
           {Opacity[.5], FaceForm[RandomColor[]], #} & /@ 
           Nest[Flatten@*iter, Polygon@#, n]} &]@
       Map[Append[p]]@ Partition[RotateRight @
          Riffle[mc, MovingAverage[Append[First@#]@# &@mc, 2]], 3, 2, 1], 
     Locator[Dynamic@p]}, ImageSize -> Large]]

Examples:

SeedRandom[777];
rp = RandomPolygon[{"Convex", 4}];

foo[rp, 1]

enter image description here enter image description here

foo[rp, 3]

enter image description here

enter image description here

Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading