29.11.2012 Views

MetaFun - Pragma ADE

MetaFun - Pragma ADE

MetaFun - Pragma ADE

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

80<br />

z6<br />

z7<br />

z1<br />

z5<br />

z4<br />

z2<br />

z3<br />

z7<br />

z6<br />

z1<br />

z5<br />

A few more details Curve construction<br />

z4<br />

z2<br />

z3<br />

Just in case you are interested in how such graphical simulations can be organized, we show simplified<br />

versions of the macros used here. (In the previous examples we minimized the complexity<br />

of the code by using buffers, but describing this mechanism is out of the scope of this section.)<br />

We need to loop over all segments of a curve, where for each segment the left and right side sub<br />

curves are handled recursively, upto the requested depth (denoted as n). For this we define the<br />

following macros.<br />

vardef dodrawmidpoints (expr a, b, c, d, n) =<br />

save e, f, g, h, i, j ; pair e, f, g, h, i, j ;<br />

e := .5[a,b] ; f := .5[b,c] ; g := .5[c,d] ;<br />

h := .5[e,f] ; i := .5[f,g] ; j := .5[h,i] ;<br />

draw j ;<br />

if n>1 :<br />

dodrawmidpoints(a, e, h, j, n-1) ;<br />

dodrawmidpoints(j, i, g, d, n-1) ;<br />

fi ;<br />

enddef ;<br />

vardef drawmidpoints (expr p, n) =<br />

save a, b, c, d ; pair a, b, c, d ;<br />

for x=0 upto length(p)-1 :<br />

a := point x of p ; b := postcontrol x of p ;<br />

d := point x+1 of p ; c := precontrol x+1 of p ;<br />

dodrawmidpoints(a, b, c, d, n) ;<br />

endfor ;<br />

enddef ;<br />

We apply this macro to a simple shape:<br />

drawmidpoints (fullcircle xscaled 300pt yscaled 50pt, 1) ;<br />

When drawn, this results in the points that makes up the curve:<br />

We now add an extra iteration (resulting in the yellow points):<br />

drawmidpoints (fullcircle xscaled 300pt yscaled 50pt, 2) ;<br />

z6<br />

z7<br />

z1<br />

z5<br />

z2<br />

z4<br />

z3<br />

z7<br />

z6<br />

z1<br />

z5<br />

z2<br />

z4<br />

z3

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

Saved successfully!

Ooh no, something went wrong!