TransWikia.com

METAPOST: a bug with clip

TeX - LaTeX Asked by Anton Petrunin on February 10, 2021

It seems that

clip currentpicture to p;

not only clips the picture to p, but also sets its the bounds in an unpredictable way.

Here is a MWE:

filenametemplate "%j-%1c.png";
outputformat := "png";
beginfig(1);
save u;
u:=1cm;

draw (-1,-1)*u--(-10,10)*u--(10,10)*u--cycle withcolor (87, 1mm, -.5bp);
clip currentpicture to (-10,-10)*u--(-10,0)*u--(10,0)*u--(10,-10)*u--cycle;

endfig;
end

The produced picture has wide empty space on its right and left, (the vertical size is as expected):

enter image description here

One Answer

I think that there is a mismatch between the OP expectation, and what Metapost actually does with a clip command. If you examine the source code — in particular the mp_set_bbox function in mp.w — you will find that the introductory comment says:

@ The major difficulty in finding the bounding box of an edge structure is the effect of clipping paths. We treat them conservatively by only clipping to the clipping path's bounding box, but this still requires recursive calls to set_bbox in order to find the bounding box of the objects to be clipped. Such calls are distinguished by the fact that the boolean parameter top_level is false.

And this is exactly what has happened in this case. The OP started of with a V-shaped path, constructed in such a way that the bbox of this path was approximately a 10cm square. This was then clipped to another 10cm square, shifted down about 9 cm. The result is a picture with a bbox that is the overlap of these two squares -- a rectangle approx 10x1cm with the bottom of the V-shape in the middle of it.

I guess there was an original design trade off here, and the designers of MP decided to keep things simple, and let the users of the language work out how to deal with edge cases for themselves.

Answered by Thruston on February 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP