What is the best way to clip a graphic to a region?



What is the best way to clip a graphic to a certain region?

Here's a very simple implementation to show what I mean:

 CountryData["World", "Shape"],
   FilledCurve[{{Line[{{-200, -100}, {200, -100}, {200, 100}, {-200, 
         100}}]}, {Line[{{-10.181224213835492, 65.15571149065886}, 
         79.14535975270482}, {14.116585925507536, 
                63.683116936759234}, {16.32547775635689, 
         35.70382041266731}, {-16.071602429433796, 
                40.85790135131583}, {-13.126413321634658, 
 PlotRange -> {{-18, 18}, {35, 82}}, Background -> LightBlue

Mathematica graphics

My aim is to be able to clip arbitrary vector graphics to an arbitrary vector region. The region can be anything, and can have curved boundaries (but approximation with line segments is okay).

The problem with the approach I showed here (i.e. simply overlaying a rectangle with a hole in it) is that it is cumbersome and it does not allow arbitrary layering of objects: imagine that I need this clipped graphic to be in the foreground and cover parts of the background objects.

Note: It is possible to clip raster graphics using Texture, as shown here. This question is about clipping arbitrary vector graphics. If it is not possible to do this in the current version of Mathematica, I accept that.


Posted 2012-02-19T16:19:41.920

Reputation: 213 047

I can only second what @YvesKlett has said, pointing to Heike's graphics intersection solution, though I know you are aware of it. It is only helpful if you have simple polygons (i.e. no disconnected parts), and still - just as Yves said - you have to make the intersection for each polygon-pair...

– István Zachar – 2012-04-10T10:37:40.363

Related: http://mathematica.stackexchange.com/q/1332/121

– Mr.Wizard – 2012-02-19T16:43:42.727

@Mr.Wizard The difference is that here I'd like to clip vector graphics, if this is possible at all. Rasterization worsens the quality and increases the file size. – Szabolcs – 2012-02-19T16:45:04.143

2Absolutely. This question is what I initially thought that one was, and IMHO should have been. Unfortunately I still don't have an answer. I think to do this properly WRI needs to add a ClippingPath function. Here's hoping there is already some undocumented function that does this. – Mr.Wizard – 2012-02-19T16:48:00.850

This would probably be doable in the old, version 5 graphics, which are based on PostScript. – Szabolcs – 2012-02-19T20:21:28.173

1@Szabolcs I don't think so. I used << Version5``Graphics`` and as far as I can tell, the PostScript engine can only deal with rectangular clipping regions. – Lev Bishop – 2012-02-20T04:24:59.313

2For current versions of Mathematica this is not possible AFAIK. You can do it the hard way by intersecting each edge of the to-be-clipped polygons with the mask polygon edges and deleting resulting outlier elements. This is a fair bit of work though. I have some hopes (and the urgent desire) of this being adressed in future versions at least for 2D cases. – Yves Klett – 2012-02-20T08:50:07.443



I'm pretty sure this can't be done. As evidence of this I put forward Import[] of .EPS and .PDF with such clipping in it: mathematica imports the shapes unclipped. If there would be some undocumented function to do this clipping, I would assume that Import[] would make use of it.

Lev Bishop

Posted 2012-02-19T16:19:41.920

Reputation: 829


You can use region functionality (RegionIntersection) to clip primitives, although it will be a bit slow. There are two issues, though.

  1. The output if not always a graphics primitive. Sometimes the output of RegionIntersection is a BooleanRegion object, and these objects don't render inside of Graphics. This can be fixed by using BoundaryDiscretizeRegion to convert to a BoundaryMeshRegion that does render inside of Graphics (in M12). If you are using earlier versions of Mathematica, you can use my answer to Make MeshRegion/BoundaryMeshRegion work as a graphics primitive in M11 to enable them to be rendered in earlier versions of Mathematica as well.

  2. 2 dimensional multi-primitives (e.g., Polygon) lose the edges where the polygons overlap. This can be fixed by converting multi-primitives to lists of single-primitives.

Here is some code that does this:

ClippedPrimitives[prims_, clip_] := prims /. r_?RegionQ :> clipPrimitives[r, clip]

clipPrimitives[p:Polygon[{__?MatrixQ}], clip_] := ClippedPrimitives[Thread[p], clip]

clipPrimitives[prim_, clip_] := Which[
    RegionDisjoint[clip, prim],

    RegionWithin[clip, prim],

        RegionIntersection[prim, clip],
        b_BooleanRegion :> BoundaryDiscretizeRegion[b]

For your example (which will be slow because the world multi-polygon consists of 5809 polygons):

    Green, Rectangle[{-18, 35}, {18, 82}],
    Red, Line[{
        First @ CountryData["World", "Shape"],

enter image description here

Carl Woll

Posted 2012-02-19T16:19:41.920

Reputation: 112 778