# Bounded polygons in Voronoi diagram and calculating the areas and the number of sides of each polygon

I have some data as

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}}


I would like to plot the Voronoi diagram of these data, so we have

Show[HighlightMesh[
VoronoiMesh[data], {Style[2, White], Style[1, Black]}],
Graphics[{PointSize[Medium], Red, Point[data]}], Frame -> False,
PlotRange -> Full]


which results in

I would like to calculate the number of sides of each polygon and also to calculate the area of them. But, before doing these stuff, how can I remove the unbounded polygons and only keep and work with the bounded ones?

vm = VoronoiMesh[data];


You can use the property "Interior" to get interior faces of vm:

interiorfaces = MeshPrimitives[vm, {2, "Interior"}];

Short[interiorfaces, 6]


interiorfaceindices = MeshCellIndex[vm, {2, "Interior"}]


HighlightMesh[vm, Style[interiorfaceindices, Red],
PlotTheme -> "Lines", BaseStyle -> Black]


Use Style[interiorfaceindices, Directive[EdgeForm[{Thick, Red}], FaceForm[]]] to get:

Construct a data set with edge counts, areas and region centroids of interiorfaces:

interiorfaceData = Map[Through@{Identity, Length@*First, Area, RegionCentroid}@# &]@
interiorfaces;


Visualize interiorfaces using area for color-coding:

Graphics[{EdgeForm[LightGray],
ColorData[{"Rainbow", MinMax @ interiorfaceData[[All, 3]]}] @ #3, #,
FontColor -> White,
Text[Style["Edge Count: "<>ToString @ #2, FontSize -> Scaled[.01]], #4, {0, -1}],
Text[Style["Area: "<>ToString @ #3, FontSize -> Scaled[.01]], #4, {0, 1}]} & @@@
interiorfaceData, ImageSize -> 800]


To get the edge counts and areas in a list:

edgecountsandareas = Map[Through@{Length@*First, Area} @ # &] @ interiorfaces


• @Banana, was missing a , after the first Text[...]. Fixed now.
– kglr
Apr 9, 2021 at 21:01
• @Banana, please see the update for the number of sides and areas in a list.
– kglr
Apr 9, 2021 at 21:12
vm = VoronoiMesh[data];
poly = MeshPrimitives[vm, "Polygons"]


First evaluate the surrounding box

b = RegionBounds[vm];
rect =Line[{{b[[1, 1]], b[[2, 1]]}, {b[[1, 2]], b[[2, 1]]}, {b[[1, 2]],b[[2, 2]]}, {b[[1, 1]], b[[2, 2]]}, {b[[1, 1]], b[[2, 1]]}}];


The intersection of rect and the polygons must be empty

rpoly=Select[poly, (RegionIntersection[#, rect] == EmptyRegion[2] &)]
Graphics[{rect,EdgeForm[Blue], FaceForm[White], rpoly}]


Number of sides of each polygon rpoly:

Map[Length@MeshPrimitives[# , "Line"] &, rpoly]
(*{4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8}*)


Areas of each polygon rpoly:

Map[Area,rpoly]
(*{3200.36, 3344.58, 4752.63, 14360.1, 6804.33, 7685.5, 1993.66,  4454.98, 9498.88, 2834.43, 12996.1, 2909.38, 6107.07, 5160.02, 7882.94, 3504.96, 4228.52, 5680.42, 6216.63, 12875.9, 4052.56,  6972.53, 3428.06, 9326.52, 9019.31, 5562.72, 11252.}*)

• @Babana Sorry, cut & paste error. I modified my answer! Apr 9, 2021 at 11:57
• @Banana See my extended answer Apr 9, 2021 at 12:47
• If the Voronoimesh exists probably yes. Apr 9, 2021 at 13:06