MetaFun - Pragma ADE

MetaFun - Pragma ADE MetaFun - Pragma ADE

pragma.ade.com
from pragma.ade.com More from this publisher
29.11.2012 Views

308 The complete logo definition is a bit more extensive because we also want to add a background. Because we need to clip the blue foreground graphic, we must temporarily store it when we fill the background. numeric width, height, line, delta ; width = 5cm ; height = width/2 ; line = height/4 ; delta = line ; linejoin := mitered ; pickup pencircle scaled line ; color nsblue ; nsblue := (0,0,1) ; color nsyellow ; nsyellow := (1,1,0) ; z1 = (0, height/2) ; z2 = (width/2-height/4, y1) ; z3 = (width/2+height/4, y4) ; z4 = (width, 0) ; z5 = (x4+height/2, y1) ; z6 = (x4, 2y1) ; z7 = 1.5[z5,z6] ; path p ; p := z1--z2--z3--z4 ; path q ; q := z3--z4--z5--z7 ; numeric d, lx, ly, ux, uy ; d = line/2 ; lx = -3d - d/3 ; ly = -d ; ux = rt x5 + d/3 ; uy = top y6 ; path r ; r := (lx,ly)--(ux,ly)--(ux,uy)--(lx,uy)--cycle; lx := lx-delta ; ly := ly-delta ; ux := ux+delta ; uy := uy+delta ; path s ; s := (lx,ly)--(ux,ly)--(ux,uy)--(lx,uy)--cycle; draw p withcolor nsblue ; draw q withcolor nsblue ; addto currentpicture also currentpicture rotatedaround (.5[z2,z3],180) shifted (height/4,height/2) ; picture savedpicture ; savedpicture := currentpicture ; clip currentpicture to r ; setbounds currentpicture to r ; savedpicture := currentpicture ; currentpicture := nullpicture ; fill s withcolor nsyellow ; addto currentpicture also savedpicture ; For practical use it makes sense to package this definition in a macro to which we pass the dimensions. A few applications Simple Logos

13.8 Music sheets The next example demonstrates quite some features. Imagine that we want to make us a couple of sheets so that we can write a musical masterpiece. Let's also forget that TEX can draw lines, which means that somehow we need to use METAPOST. Drawing a bar is not that complicated as the following code demonstrates. \startusableMPgraphic{bar} vardef MusicBar (expr width, gap, linewidth, barwidth) = image ( interim linecap := butt ; for i=1 upto 5 : draw ((0,0)--(width,0)) shifted (0,(i-1)*gap) withpen pencircle scaled linewidth ; endfor ; for i=llcorner currentpicture -- ulcorner currentpicture , lrcorner currentpicture -- urcorner currentpicture : draw i withpen pencircle scaled barwidth ; endfor ; ) enddef ; \stopusableMPgraphic We can define the sidebars a bit more efficient using two predefined subpaths: for i=leftboundary currentpicture, rightboundary currentpicture : We define a macro MusicBar that takes four arguments. The first two determine the dimensions, the last two concern the line widths. Now watch how we can use this macro: \includeMPgraphic{bar} ; draw MusicBar (200pt, 6pt, 1pt, 2pt) ; draw MusicBar (300pt, 6pt, 1pt, 2pt) shifted (0,-30pt) ; As you can see in this example, the bar is a picture that can be transformed (shifted in our case). However, a close look at the macro teaches us that it does a couple of draws too. This is possible because we wrap the whole in an image using the image macro. This macro temporary saves the current picture, and at the end puts the old currentpicture under the new one. We wrap the whole in a vardef. This means that the image is returned as if it was a variable. Actually, the last thing in a vardef should be a proper return value, in our case a picture. This also means that we may not end the vardef with a semi colon. So, when the content of the vardef is expanded, we get something draw some_picture ... ; Music sheets A few applications 309

308<br />

The complete logo definition is a bit more extensive because we also want to add a background.<br />

Because we need to clip the blue foreground graphic, we must temporarily store it when we fill<br />

the background.<br />

numeric width, height, line, delta ;<br />

width = 5cm ; height = width/2 ; line = height/4 ; delta = line ;<br />

linejoin := mitered ; pickup pencircle scaled line ;<br />

color nsblue ; nsblue := (0,0,1) ;<br />

color nsyellow ; nsyellow := (1,1,0) ;<br />

z1 = (0, height/2) ;<br />

z2 = (width/2-height/4, y1) ;<br />

z3 = (width/2+height/4, y4) ;<br />

z4 = (width, 0) ;<br />

z5 = (x4+height/2, y1) ;<br />

z6 = (x4, 2y1) ;<br />

z7 = 1.5[z5,z6] ;<br />

path p ; p := z1--z2--z3--z4 ; path q ; q := z3--z4--z5--z7 ;<br />

numeric d, lx, ly, ux, uy ; d = line/2 ;<br />

lx = -3d - d/3 ; ly = -d ; ux = rt x5 + d/3 ; uy = top y6 ;<br />

path r ; r := (lx,ly)--(ux,ly)--(ux,uy)--(lx,uy)--cycle;<br />

lx := lx-delta ; ly := ly-delta ; ux := ux+delta ; uy := uy+delta ;<br />

path s ; s := (lx,ly)--(ux,ly)--(ux,uy)--(lx,uy)--cycle;<br />

draw p withcolor nsblue ; draw q withcolor nsblue ;<br />

addto currentpicture also currentpicture<br />

rotatedaround (.5[z2,z3],180) shifted (height/4,height/2) ;<br />

picture savedpicture ; savedpicture := currentpicture ;<br />

clip currentpicture to r ;<br />

setbounds currentpicture to r ;<br />

savedpicture := currentpicture ; currentpicture := nullpicture ;<br />

fill s withcolor nsyellow ;<br />

addto currentpicture also savedpicture ;<br />

For practical use it makes sense to package this definition in a macro to which we pass the dimensions.<br />

A few applications Simple Logos

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!