Please, put this example on the wiki (together with some sample images that can be created, with or without some cheating). The community would be greatful for that.
Hans, Taco, et al,
This is getting pretty close to the limit of my expertise. The good stuff of it is stolen from Metafun and mp-func.mp.
Because of the way I handle clipping the path,
p:=p cutbefore cpath; p:=reverse p; p:=p cutbefore cpath;
this will only work on functions (like lines, quadratics, and polynomials, maybe sinusoids) that both enter and leave the region inside the clipping path.
If I understand it properly (that you only want to draw the content inside the square, while the function may be a circle or something lying completely outside the region), you could also use something like clip currentpicture to cpath; although you may have to change the code that draws double arrow then. Mojca