Can a voronoi diagram be clipped to some arbitrary shape in Plot?

Is there a way of clipping a (geographic) Voronoi diagram within Plot to some arbitrary shape? By default it clips to a bounding rectangle. If I set the domain to come arbitrary geo shape, the Voronoi is clipped to its bounding rectangle, not its actual shape. If, for computational reasons, the clipping region had to be convex, that would still be helpful.

I realise can can use d3.delaunay and other JavaScript to create a geoJSON with the clipped Voronoi diagram, but ideally I’d like to be able to do this as a viewing operation within Plot itself.

1 Like

Making a geometric clipping is a bit of work. Though Sutherland-Hodgman can work with any shape in this case, because the voronoi cells are convex.

However in most cases it’s enough to do svg clipping; you just need to somehow draw the geo shape into a clipPath. Here’s an example with a (very simple) custom render transform: DIY live map of Air quality in the US / Fil | Observable (it’s clipping a contour mark, not a voronoi mark, but this makes no difference: just replace Plot.contour with Plot.voronoi to see).

Here’s another example, showing how to clip to the sphere of the projection: Plot: Voronoi map / Observable | Observable

I hope that at some point we can make this easier:

2 Likes

Thanks for all those pointers, which are helpful.

The SVG clipPath option looks promising for a view-based solution. For now I may stick to clipping at the data level as this forms part of a workflow I have for generating area maps from point sets. For info, I provided a worked example which also acts as a use-case for clipping Voronoi cells as map units:

Wow, that’s a great notebook! I forgot to mention that if you need a generic clip (even non-convex against non-convex), you can use mfogel’s polygon-clipping implementation (see Hello, polygon-clipping / Fil | Observable and Clip Lloyd / Fil | Observable).

1 Like