Assigning a Color to each Point on the Plane
Convex hull mesh
coords = CoordinateBoundsArray[{{-10, 10}, {-10, 10}}];
in = Select[Flatten[coords, 1], Norm@# <= 5 &];
out = Select[Flatten[coords, 1], Norm@# > 5 &];
p1 = Cases[Normal@ConvexHullMesh[out]["Graphics"], _Polygon, Infinity];
p2 = Cases[Normal@ConvexHullMesh[in]["Graphics"], _Polygon, Infinity];
Graphics[{
ColorData[97, 1], First@p1,
ColorData[97, 2], First@p2
}]
If anybody knows of a better way to turn a convex hull mesh into a polygon, please let me know.
Interpolation
Interpolation with interpolation order 0.
data = Join[{#, 1} & /@ in, {#, 2} & /@ out];
interp = Interpolation[data, InterpolationOrder -> 0];
DensityPlot[
interp[x, y],
{x, -10, 10},
{y, -10, 10},
PlotPoints -> 100
]
Using a higher interpolation order and rounding to get smoothed boundaries:
interp = Interpolation[data, InterpolationOrder -> 1];
DensityPlot[
Round@interp[x, y],
{x, -10, 10},
{y, -10, 10},
PlotPoints -> 100
]
VoronoiMesh method
points = Drop[data, None, -1];
mesh = VoronoiMesh[points];
polygons = MeshPrimitives[mesh, 2];
coloredpolygons = Map[{incircle @@ RegionCentroid[#], #} &, polygons];
Graphics[coloredpolygons]
VoronoiMesh
automatically chooses the coordinates of the boundaries of the mesh, but you can also specify the boundaries via a second argument (VoronoiMesh[points, {{xmin, xmax}, {ymin, ymax}}]
) if you want.
This method generalizes nicely to irregularly located points as well.
points = RandomReal[{-10, 10}, {200, 2}];
(* Remaining code as above *)
Note, however, that there is a bug in this code: the centroid of a Voronoi cell is not necessarily its "base point". This may make a difference in some cases, particularly if your base points are not uniformly distributed.
I will have to think if there's an easy way to color the Voronoi cells based on their base point, rather than their centroid. Complicating matters is that MeshPrimitives[VoronoiMesh[points]]
appears to shuffle the order of the resulting polygons (i.e., the $i$th polygon in the result does not necessarily contain the $i$th element of points
.)