1

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

9
  • What do you mean by “right”?
    – egreg
    Aug 13 '20 at 7:58
  • @egreg the size should be calculated ignoring empty space (as it happens usually). Aug 14 '20 at 2:29
  • 3
    OT: that color definition is a bit weird....
    – Aditya
    Aug 19 '20 at 0:49
  • @Aditya the withcolor appears to be left over from one used with the hatching package.
    – Thruston
    Aug 21 '20 at 21:57
  • I don't think that there is anything wrong here. It has set the bounds to the overlap of the bbox of the currentpicture and the path that you have given. What else where you expecting? That it would automatically shrink the bbox after it has done the clip?
    – Thruston
    Aug 21 '20 at 22:14
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.