In MetaPost, I would like to fill in the region in between non-intersecting shapes with a color, similar to how a “bucket fill” tool works in an image editing program. Consider the following code:
xxxxxxxxxx
beginfig(1);
path a,b,c;
a = fullcircle scaled 200;
b = fullcircle scaled 70 shifted (40,40);
c = fullcircle scaled 40 shifted (-20,20);
draw a;
draw b;
draw c;
endfig;
From this code, I would like to produce this:
I have tried using the unfill
command (i.e., fill a; unfill b; unfill c;
), but that command makes the smaller circles white rather than transparent. So if I open the image using a graphics editor program which shows transparency as a checkerboard pattern, I get this:
I have also tried doing fill a--reverse b--reverse c--cycle;
but that also does not make the negative space transparent, and also does not render the shapes properly because of how the paths are connected.
How do I make this figure in a way so that the smaller circles are transparent?
The problem has been addressed in the comments section for the particular circles whose code was provided in the question.
It was subsequently asked if there is a method that would work for, say, 100 holes cut into the large disc. Extending the trick used for two holes to a large number of holes doesn’t seem straightforward nor elegant, and I can’t see another way with MetaPost—which does not mean there is none. Therefore, I propose TikZ/expl3 code that does the job for an arbitrary number of circles (precisely 100 in the given example, but it is all parametrized).
The main function is \computeHoleLocations
; it accepts the following parameters (a key-value interface would clearly be more convenient, but this is not the point of the exercise):
xxxxxxxxxx
#1: node name for the center of the enclosing disc
#2: radius of the enclosing disc, in the xy-coordinate system
#3: minimum hole radius
#4: maximum hole radius
#5: number of holes
#6: minimum distance between holes, in the xy-coordinate system
#7: minimum distance between a hole and the outer circle, in the
xy-coordinate system
Any two holes are guaranteed to have at least #6 of space between them. Any hole is guaranteed to lie at least #7 from the border of the background disc.
(If you ask for something difficult or just impossible, TeX will try very hard; compilation may take a long time in such cases!)