10

Consider the following:

SeedRandom["blah"];
pts = RandomReal[1, {50, 2}];

{cm = ConvexHullMesh[pts],
 vm = VoronoiMesh[pts, Epilog -> {Red, PointSize[Small], Point[pts]}]} // GraphicsRow

convex hull and Voronoi diagram

I want to generate the Voronoi diagram bounded by the convex hull. I had tried using RegionIntersection but all I get is the original convex hull. Is there a way to cut out the parts of the Voronoi diagram that are outside the convex hull, such that the result is still a MeshRegion object?

joe khool
  • 101
  • 3

1 Answers1

14

Here is an approach that should get you there:

SeedRandom["blah"]; pts = RandomReal[1, {50, 2}];
cm = ConvexHullMesh[pts];
vm = VoronoiMesh[pts, Epilog -> {Red, PointSize[Small], Point[pts]}];

The idea is to find the RegionIntersection of the Voronoi cells with the convex hull of the sites, and to combine these regions to get the Voronoi diagram bounded by the convex hull:

prim = MeshPrimitives[vm, 2];
allreg = RegionIntersection[cm, BoundaryDiscretizeRegion @ #] & /@ prim;
allprim = Map[MeshPrimitives[#, 2] &, allreg][[All, 1]];

We now have the Voronoi cells bounded by the convex hull. Let's visualize it:

gr = Graphics[{{EdgeForm[Black], RandomColor[], #} & /@ allprim, Point[pts]}]

Mathematica graphics

Together with the original Voronoi diagram:

Show[vm, gr]

Mathematica graphics

To obtain a MeshRegion from the bounded Voronoi diagram we simply do the following:

reg = DiscretizeGraphics @ RegionPlot @ allreg

Mathematica graphics

We can check that it is indeed a MeshRegion:

{MeshRegionQ[reg], Head[reg]}

{True, MeshRegion}

Update

Here is a simplified approach thanks to @J.M.:

SeedRandom["blah"]; pts = RandomReal[1, {50, 2}];
cm = ConvexHullMesh[pts];
vm = VoronoiMesh[pts];
ch = MeshPrimitives[cm, 2][[1]];
DiscretizeGraphics[RegionIntersection[ch, #] & /@ MeshPrimitives[vm, 2]]
RunnyKine
  • 33,088
  • 3
  • 109
  • 176