2
$\begingroup$

My primary data is

data = {{257.3`, 493.7`}, {43.666666666666664`,490.5`}, {111.91176470588235`,461.20588235294116`},{345.2142857142857`,460.5`}, {420.88461538461536`, 436.34615384615387`}, {318.1`,408.46`}, {277.`,400.7`}, {273.5`, 383.`}, {444.`,381.5`}, {208.28571428571428`,379.7857142857143`}, {510.9166666666667`,367.66666666666663`}, {584.7`, 366.`}, {301.125`, 355.5`}, {116.875`, 352.75`}, {423.14285714285717`,340.2142857142857`}, {360.2142857142857`,340.07142857142856`}, {234.7`, 318.1`}, {287.25`,303.25`}, {474.65`, 301.35`}, {110.71428571428571`, 299.5`}, {440.95714285714286`, 297.3`}, {536.4545454545455`,277.72727272727275`}, {321.`, 268.2142857142857`}, {439.3125`,249.875`}, {306.42857142857144`,242.78571428571428`}, {505.42857142857144`,242.10714285714286`}, {603.5370370370371`,217.3888888888889`}, {618.0909090909091`, 212.86363636363637`}, {248.5`, 212.875`}, {110.07894736842105`, 199.60526315789474`}, {384.7857142857143`, 188.`}, {572.8333333333334`,148.66666666666669`}, {33.`, 133.`}, {447.31481481481484`, 129.24074074074076`}, {206.33333333333334`, 116.41666666666669`}, {399.1764705882353`, 98.35294117647061`}, {33.3`,60.366666666666674`}, {216.875`, 44.`}, {328.77272727272725`, 40.31818181818181`}, {435.5`,38.5`}, {58.88461538461539`,37.11538461538464`}, {464.44117647058823`,23.323529411764696`}, {534.`, 1.375`}}

To plot Voronoi diagram of this set of data we have:

vm = VoronoiMesh[data];
interiorfaces = MeshPrimitives[vm, {2, "Interior"}];
Graphics[{EdgeForm[White], {Yellow, interiorfaces}}]

which results in:

enter image description here

Now, I want to break my primary data to two sets, and to have different colors for each set in Voronoi diagram.

data1 = {{257.3`, 493.7`}, {43.666666666666664`, 490.5`}, {111.91176470588235`, 461.20588235294116`}, {345.2142857142857`, 460.5`}, {420.88461538461536`, 436.34615384615387`}, {318.1`, 408.46`}, {277.`, 400.7`}, {273.5`, 383.`}, {444.`, 381.5`}, {208.28571428571428`, 379.7857142857143`}, {510.9166666666667`, 367.66666666666663`}, {584.7`, 366.`}, {301.125`, 355.5`}, {116.875`, 352.75`}, {423.14285714285717`,340.2142857142857`}, {360.2142857142857`, 340.07142857142856`}, {234.7`, 318.1`}, {287.25`,303.25`}, {474.65`, 301.35`}, {110.71428571428571`,299.5`}, {440.95714285714286`, 297.3`}, {536.4545454545455`,277.72727272727275`}, {321.`, 268.2142857142857`}, {439.3125`,249.875`}, {306.42857142857144`,242.78571428571428`}, {505.42857142857144`, 242.10714285714286`}};

vm1 = VoronoiMesh[data1];
interiorfaces1 = MeshPrimitives[vm1, {2, "Interior"}];

data2 = {{603.5370370370371`,217.3888888888889`}, {618.0909090909091`, 212.86363636363637`}, {248.5`, 212.875`}, {110.07894736842105`,199.60526315789474`}, {384.7857142857143`, 188.`}, {572.8333333333334`, 148.66666666666669`}, {33.`,133.`}, {447.31481481481484`, 129.24074074074076`}, {206.33333333333334`,116.41666666666669`}, {399.1764705882353`, 98.35294117647061`}, {33.3`, 60.366666666666674`}, {216.875`,44.`}, {328.77272727272725`, 40.31818181818181`}, {435.5`,38.5`}, {58.88461538461539`, 37.11538461538464`}, {464.44117647058823`, 23.323529411764696`}, {534.`, 1.375`}};

vm2 = VoronoiMesh[data2];
interiorfaces2 = MeshPrimitives[vm2, {2, "Interior"}];

Graphics[{EdgeForm[White], {{Brown, interiorfaces1}, {Blue, interiorfaces2}}}]

which results in

enter image description here

Besides the fact that some parts are missing, it also seems that even if the missing parts, in some way, to be added, one cannot get the original Voronoi plot. How can this be fixed?

$\endgroup$
1
$\begingroup$
ClearAll[nearestInteriorFaceIndices, nearestInteriorFaces]

nearestInteriorFaceIndices[mr_, pts_] := 
  Intersection[MeshCellIndex[mr, {2, "Interior"}], 
   Join @@ (NearestMeshCells[mr, #] & /@ pts)];

nearestInteriorFaces[mr_, pts_] := 
  MeshPrimitives[mr, nearestInteriorFaceIndices[mr, pts]];

HighlightMesh[vm, {Style[nearestInteriorFaceIndices[vm, data1], Red], 
  Style[nearestInteriorFaceIndices[vm, data2], Green]}]

enter image description here

Graphics[{EdgeForm[White], 
  Red, nearestInteriorFaces[vm, data1], 
  Green, nearestInteriorFaces[vm, data2]}]

enter image description here

Note: In versions older than version 12.1, you can use

ClearAll[nearestInteriorFaceIndices]

nearestInteriorFaceIndices[mr_, pts_] := 
   Intersection[MeshCellIndex[mr, {2, "Interior"}],
      Region`Mesh`MeshNearestCellIndex[mr, pts]]; 
$\endgroup$
0
3
$\begingroup$
data1 = RandomReal[{-10, 0}, {100, 2}];
data2 = RandomReal[{0, 10}, {100, 2}];
data = Join[data1, data2];
n = Length[data];
k = Length[data1];
vm = VoronoiMesh[data];
Polys = MeshPrimitives[vm, 2];
inPolys = MeshPrimitives[vm, {2, "Interior"}];
outPolys = Complement[Polys, inPolys];
rerangePolys = 
  Polys . (RegionMember[#, data] & /@ Polys /. {False -> 0, 
      True -> 1});
Graphics[{EdgeForm[White], {Purple, 
   Complement[Table[rerangePolys[[i]], {i, 1, k}], outPolys]}, {Brown,
    Complement[Table[rerangePolys[[i]], {i, k + 1, n}], 
    outPolys]}, {LightGreen, outPolys}}]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ You can replace /. {False -> 0, True -> 1} with //Boole $\endgroup$ Apr 10 at 14:44
  • $\begingroup$ @OkkesDulgerci Thanks! $\endgroup$
    – cvgmt
    Apr 10 at 14:50
2
$\begingroup$

How about this?

findCorrespondingPolygon[pt_] := Select[
  interiorfaces,
  RegionMember[#, pt] &
  ]

region1 = Cases[
   findCorrespondingPolygon /@ data1,
   _Polygon,
   {2}
   ];

region2 = Cases[
   findCorrespondingPolygon /@ data2,
   _Polygon,
   {2}
   ];

Graphics[{
  EdgeForm[White],
  {{Brown, region1}, {Blue, region2}}
  }]

Mathematica graphics

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy