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.

222<br />

Writing such LUA functions is no big deal. For instance we have available:<br />

function metapost.metafun.interpolate(f,b,e,s,c)<br />

tex.write("(")<br />

for i=b,e,(e-b)/s do<br />

local d = loadstring(string.format("return function(x) return %s end",f))<br />

if d then<br />

d = d()<br />

if i > b then<br />

tex.write(c or "...")<br />

end<br />

tex.write(string.format("(%s,%s)",i,d(i)))<br />

end<br />

end<br />

tex.write(")")<br />

end<br />

An example of usage is:<br />

draw<br />

\ctxlua{metapost.metafun.interpolate(<br />

"math.sin(x^2+2*x+math.sqrt(math.abs(x)))",<br />

-math.pi/2,math.pi/2,100<br />

)}<br />

xysized(6cm,3cm)<br />

withpen pencircle scaled 1mm<br />

withcolor .625 red ;<br />

And indeed we get some drawing:<br />

Let's see what happens when we use less accuracy and a different connector:<br />

draw<br />

\ctxlua{metapost.metafun.interpolate(<br />

"math.sin(x^2+2*x+math.sqrt(math.abs(x)))",<br />

-math.pi/2,math.pi/2,10,"--"<br />

)}<br />

xysized(6cm,3cm)<br />

withpen pencircle scaled 1mm<br />

withcolor .625 red ;<br />

Now we get:<br />

Functions Drawing functions

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

Saved successfully!

Ooh no, something went wrong!