add tag
a year ago Trevor

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:

mp_question_0.png

From this code, I would like to produce this:

mp_question_1.png

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:

mp_question_2.png

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?

Top Answer
a year ago frougon

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).

Screenshot_2024-04-01_00-44-55.png

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):

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!)

a year
Trevor replying to frougon — Friday, 29th Mar 2024 17:48

My bad, you’re right there was a mismatch between my code and my images. I have updated the code in my post.

I wonder if it’s possible to post-process the pdf using another program that converts everything that’s colored white to transparent. I wish Metapost supported RGBA colors.

10 hours
frougon replying to Trevor — Friday, 29th Mar 2024 07:53

No, I used the exact same circles as in the provided code. It is your images that have different circles (in your code, it is quite clear that the center of the smallest circle is located south-west of the center of the largest one—contrary to what your images show).

With two arbitrary, non-intersecting “small circles”, the problem seems doable (using e.g. the radical axis of the two smaller circles). With 100 or an arbitrary number of non-intersecting smaller circles, I don’t know. Trying to generalize the above technique to solve this may be an interesting mathematical problem (graph theory?), unfortunately this exceeds by far the little amount of time I can spend here…

8 hours
Trevor — Thursday, 28th Mar 2024 23:57

@frougon So you changed the location of the circles to avoid having the path go through those circles? Is there a way to do this that doesn’t involve manual inspection of the circles? Ideally I am looking for a solution that would also work if I had 100 small circles instead of 2.

a day
frougon — Wednesday, 27th Mar 2024 14:59

The same in PDF format can be obtained by compiling with lualatex:

frougon — Wednesday, 27th Mar 2024 14:51

The following, directly compiled with mpost, is based on the provided code and works:

blob

Provided screenshots have the circles placed differently, though—don’t correspond to the provided code. Similar gymnastics could certainly be used, but it has to correspond to the precise placement of circles.

3 hours
Trevor — Wednesday, 27th Mar 2024 11:45

@samcarter Vector graphic such as pdf

3 hours
samcarter — Wednesday, 27th Mar 2024 09:03

@Trevor, re: your question, What is your target file format? Do you need the result as vector graphic or as pixel graphic?

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.