4
$\begingroup$

I want to calculate the total length of edges in a Voronoi diagram like this Voronoi

I can calculate this with

lengths = RegionMeasure /@ MeshPrimitives[VoronoiMesh[pts], 1];

Total[lengths]

but, I want to eliminate from the calculation the diagrams touching the border of the image. I can select this diagrams like here

Voronoi interior

and I can keep with the orange diagrams

selected cells

but I don't know how to calculate the total length of the edges of the last graphic. Any suggestions?

$\endgroup$
  • 1
    $\begingroup$ Can you add the code to select the inner cells and the definition of pts? $\endgroup$ – MarcoB Mar 7 at 21:01
  • $\begingroup$ The distribution of points pts comes from a picture but could be random numbers. For example: SeedRandom[332] pts = RandomReal[1, {100, 2}]; xy = VoronoiMesh[pts, {{0, 1}, {0, 1}}]; i2 = MeshCellIndex[xy, {2, "Interior"}] ; HighlightMesh[xy, Style[i2, LightOrange]] $\endgroup$ – Mati Ger Mar 7 at 21:40
7
$\begingroup$
SeedRandom[1]
pts = RandomReal[1, {100, 2}];
vm = VoronoiMesh[pts];

"Interior"

HighlightMesh[vm, Style[MeshCellIndex[vm, {1, "Interior"}], Red]]

enter image description here

Total[ArcLength /@ MeshPrimitives[vm , {1, "Interior"}]]

19.4739

Alternatively,

Total[RegionMeasure /@ MeshPrimitives[vm, {1, "Interior"}]]

19.4739

RegionMeasure[MeshRegion[MeshCoordinates[vm], MeshCells[vm, {1, "Interior"}]]]

19.4739

"Boundary"

HighlightMesh[vm, Style[MeshCellIndex[vm, {1, "Boundary"}], Red]]

enter image description here

Total[ArcLength /@ MeshPrimitives[vm, {1, "Boundary"}]]

5.92015

"Frontier"

HighlightMesh[vm, Style[MeshCellIndex[vm, {1, "Frontier"}], Red]]

enter image description here

Total[ArcLength /@ MeshPrimitives[vm, {1, "Frontier"}]]

5.357575

All lines

Total[vm["EdgeLengths"]]

30.75167

Total[ArcLength /@ MeshPrimitives[vm , 1]]

30.751672460568727

$\endgroup$
  • $\begingroup$ So long as none of the cells adjacent to the boundary aren't in fact the full Voronoi cell. This would only happen in contrived cases though. $\endgroup$ – Chip Hurst Mar 7 at 21:23
  • $\begingroup$ @ChipHurst, good point. $\endgroup$ – kglr Mar 7 at 21:25
  • $\begingroup$ I am impressed by the speed and clarity in your response, kglr. I am very grateful with you. Thank you. $\endgroup$ – Mati Ger Mar 7 at 21:48
  • $\begingroup$ @MatiGer, my pleasure. Thank you for the kind words and accept. $\endgroup$ – kglr Mar 7 at 22:00

Your Answer

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

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