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.
I proceed the in the order I do because I want the axes layered atop
the grid and the functions layered atop the grid and axes.
It's actually a pretty cool routine for teachers as it almost works
like a TI83 calculator. Just change xmin, xmax, xscl, ymin, ymax, and
yscl to determine domain and range and tick marks, then adjust the
number of points for a smooth graph, then set the width of the figure
you want. There's an option to label the axes differently from the
usual x and y, if needed.
Finally, thanks to Hans' expertise, the drawfcn routine allows for a
parametric definition of the intended curve.
All you need to do to make another graphic is copy and paste
everything between \startMPpage ... \stopMPpage and adjust the
parameters described above. Now you have a second plot.
You can run texexec with
texexec filename
or
texexec --page=2 filename
if you only want your second graphic. When I compile all, I write an
xml database file (very cool) and use
\usemodule[fig-base]
\usefigurebase[figures/figlibSection1]
in my document. Very cool working arrangement.
Here's some lines from my xml file:
<figurelibrary language="en">
<description>
<organization>College of the Redwoods Mathematics Department</
organization>
<project>Intermediate Algebra Text</project>
<product>Chapter 1</product>
<comment>Figures for Section 1</comment>
</description>
<figure>
<file>section1figs-mpgraph.1</file>
<label>yeqx2</label>
<copyright>College of the Redwoods Mathematics Department</copyright>
<comment></comment>
</figure>
<figure>
<file>section1figs-mpgraph.2</file>
<label>yeq2x2</label>
<copyright>College of the Redwoods Mathematics Department</copyright>
<comment></comment>
</figure>
...
</figurelibrary>
And here's my function machine. I'd love to hear any suggestions for
improvement. Thanks to all who have helped me the last week (Taco has
been especially patient). I've learned a lot.
%output=pdf
\setupcolors[state=start]
\definecolor[gridlines][s=0.7]
\startMPinclusions
color gridlines; gridlines:=\MPcolor{gridlines};
vardef create_grid (expr l,r,h,b,t,v,wid,ht)=
save ux, uy; numeric ux, uy;
(r-l)*ux=wid; (t-b)*uy=ht;
for k=b step v until t:
draw (l*ux,k*uy)--(r*ux,k*uy) withcolor \MPcolor{gridlines};
endfor;
for k=l step h until r:
draw (k*ux,b*uy)--(k*ux,t*uy) withcolor \MPcolor{gridlines};
endfor;
enddef;
vardef create_axes (expr l,r,b,t,wid,ht) (text xlbl,ylbl) =
save ux, uy; numeric ux, uy;
(r-l)*ux=wid; (t-b)*uy=ht;
textextoffset:=3pt;
drawdblarrow (1.05*l*ux,0)--(1.05*r*ux,0);
draw textext.rt(xlbl) shifted (1.05*r*ux,0);
draw textext.bot(decimal r) shifted (r*ux,0);
drawdblarrow (0,1.05*b*uy)--(0,1.05*t*uy);
draw textext.top(ylbl) shifted (0,1.05*t*uy);
draw textext.lft(decimal t) shifted (0,t*uy);
enddef;
vardef drawfcn (expr ind,dep,l,r,b,t,wid,ht,n) text txt =
save x, dx, ux, uy; numeric x, dx, ux, uy;
dx:=(r-l)/n;
(r-l)*ux=wid; (t-b)*uy=ht;
save cpath; path cpath; cpath:=(l,b)--(r,b)--(r,t)--(l,t)--cycle;
save p; path p; hide (x:=l;) p:=(scantokens(ind),scantokens(dep));
for xx:=l step dx until r:
hide (x:=xx;) p:=p--(scantokens(ind),scantokens(dep));
endfor;
hide(x:=r;) p:=p--(scantokens(ind),scantokens(dep));
p:=p cutbefore cpath;
p:=reverse p;
p:=p cutbefore cpath;
p:=p xyscaled (ux,uy);
drawdblarrow p txt;
enddef;
\stopMPinclusions
\starttext
\startMPpage
%initialize window parameters
numeric xmin, xmax, xscl, ymin, ymax, yscl;
xmin:=-10;
xmax:=10;
xscl:=1;
ymin:=-10;
ymax:=10;
yscl:=1;
%initialize number of points
numeric num_points;
num_points:=100;
%initialize dimensions of image
numeric width, height;
width=3in;
height=3in;
%create the grid
create_grid(xmin,xmax,xscl,ymin,ymax,yscl,width,height);
%create the axes
create_axes(xmin,xmax,ymin,ymax,width,height)("$x$")("$y$");
%draw the function
%drawf(xmin,xmax,ymin,ymax,width,height,num_points);
drawfcn("x","x*x",xmin,xmax,ymin,ymax,width,height,100) ;
%to add another function with extra formatting, try:
% drawfcn("x","(x+1)*(x+1)",xmin,xmax,ymin,ymax,width,height,100)
% withpen pencircle scaled 2pt dashed evenly withcolor red;
\stopMPpage
\stoptext
%%% Local Variables:
%%% mode: conTeXt-en
%%% End: