I have just began using MetaPost and I would liked to be able to put a grid on my images, so I could easily read coordinates from it. I would liked to write a macro, that I could import while creating new figures and draw a grid with a single command.
My grid macro is the following:
def drawgrid(expr width, height, nx, ny) = begingroup ny * u = width; nx * v = height; lu = 2 * u; lv = 2 * v; my = floor(width / lu); mx = floor(height / lv); for y = 0 upto nx: draw (0,y*v)--(width,y*v); endfor for y = 0 upto mx: draw (0,y*lv)--(width,y*lv) withpen pencircle scaled 2; label(decimal(y*lv/v), (-0.5u, y*lv)); endfor for x = 0 upto my: draw (x*lu,0)--(x*lu,height) withpen pencircle scaled 2; label(decimal(x*lu/u), (x*lu, -0.5v)); endfor for x = 0 upto ny: draw (x*u,0)--(x*u,height); endfor endgroup enddef; end
It works fine, when called from the same file (grid.mp), but fails to draw anything if I call it from my another figure. My other figures code is:
input grid beginfig(1); textwidth := 345.0; width := 0.5textwidth; radius := 0.25width; fill fullcircle scaled radius withcolor blob; draw fullcircle scaled radius withcolor blobBorder; clip currentpicture to (-0.5,-0.5)--(-0.5,0.5radius+0.5)--(0.5radius+0.5,0.5radius+0.5)--(0.5radius+0.5,-0.5)--cycle; draw (-0.5, 0.5radius)--(-0.5, -0.5)--(0.5radius, -0.5) withcolor blobBorder; draw (0, 0)--(0.25radius, 0.45radius); drawgrid(width,width,2,2); endfig end
It does not draw anything (even do not produce an .1 file). What am I doing wrong?