6
$\begingroup$

I have used Voronoi mesh to generate a mesh of a group of random numbers,

pts = RandomReal[{-1, 1}, {8, 2}];
mesh = VoronoiMesh[pts]

So, it will generate a graph similar to this:

Voronoi

I want to calculate the area of each mesh grid. Is there any function that can be used to do that? Thanks.

$\endgroup$
1
  • $\begingroup$ Area /@ MeshPrimitives[mesh,2] $\endgroup$
    – Jason B.
    Commented Jun 23, 2016 at 23:54

1 Answer 1

9
$\begingroup$

This is a MeshRegion,

SeedRandom[420];
pts = RandomReal[{-1, 1}, {8, 2}];
mesh = VoronoiMesh[pts]

You could get the individual polygons with MeshPrimitives and then use Area to find the area. This was my first thought, but the comments below made me realize it is much faster to use the built-in functions PropertyValue and MeshCellMeasure. So at the expense of a longer code, this is what we'll use

PropertyValue[{mesh, 2}, MeshCellMeasure]
(* {0.439766, 0.51057, 0.373666, 0.494299, 0.734753, 0.387787, 
1.10474, 1.01007} *)

As pointed out by J.M. can get the same thing with the built-in, but slightly longer, PropertyValue[{mesh, 2}, MeshCellMeasure]. You can label the cells with the area using HighlightMesh

HighlightMesh[mesh, 
 MapIndexed[Labeled[{2, First@#2}, #1] &, 
  PropertyValue[{mesh, 2}, MeshCellMeasure]]]

Mathematica graphics

$\endgroup$
7
  • $\begingroup$ PropertyValue[{mesh, 2}, MeshCellMeasure] will also return the list of areas. $\endgroup$ Commented Jun 24, 2016 at 0:23
  • $\begingroup$ Thanks, I never look for a built-in when I can work out how to do something. $\endgroup$
    – Jason B.
    Commented Jun 24, 2016 at 1:01
  • 3
    $\begingroup$ Using the MeshCellMeasure approach will be faster for large computations, as it does not have to call the evaluator. $\endgroup$
    – user21
    Commented Jun 24, 2016 at 2:12
  • 2
    $\begingroup$ @JasonB, no they are calculated when requested. But then they are computed in one go. In the Area approach each is cell is passed to Area and that is equivalent to calling the evaluator. Possibly calling Area /@... will also unpack the data. $\endgroup$
    – user21
    Commented Jun 24, 2016 at 2:18
  • 1
    $\begingroup$ @JasonB, basically internally MeshCellMeasure calls a C code version of Area[allcells]. On top level things were a bit better if one could call something like Area[MeshPrimitives[mesh, 2][[All, 1]]] $\endgroup$
    – user21
    Commented Jun 24, 2016 at 2:38

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.