ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* It works, passing text
@ 2005-12-27 20:41 David Arnold
  0 siblings, 0 replies; only message in thread
From: David Arnold @ 2005-12-27 20:41 UTC (permalink / raw)


Taco,

I have something that works. Thanks for hanging in there with me.

%output=pdf

\setupcolors[state=start]

\definecolor[gridlines][s=0.7]

\startMPinclusions

color gridlines; gridlines:=\MPcolor{gridlines};

vardef create_grid(expr xxmin,xxmax,xxscl,yymin,yymax,yyscl,uux,uuy)=
   for k=xxmin step xxscl until xxmax:
     draw (xmin*uux,k*uuy)--(xmax*uux,k*uuy) withcolor \MPcolor 
{gridlines};
     draw (k*uux,ymin*uuy)--(k*uux,ymax*uuy) withcolor \MPcolor 
{gridlines};
   endfor;
enddef;

vardef create_axes (expr xxmin,xxmax,yymin,yymax,uux,uuy) (text  
xlbl,ylbl) =
   textextoffset:=3pt;
   picture p; p:=textext.rt(xlbl); p:=p shifted (1.05*xxmax*ux,0);
   picture q; q:=textext.top(ylbl); q:=q shifted (0,1.05*yymax*uy);
   drawdblarrow (1.05*xxmin*uux,0)--(1.05*xxmax*uux,0);
   draw p;
   drawdblarrow (0,1.05*yymin*uuy)--(0,1.05*yymax*uuy);
   draw q;
enddef;

vardef drawf(expr xxmin,xxmax,yymin,yymax,uux,uuy,n)=
   path p; p:=(xxmin,f(xxmin));
   numeric dx; dx=(xxmax-xxmin)/n;
   for x=xmin step dx until xxmax:
     p:=p--(x,f(x));
   endfor;
   p:=p--(xxmax,f(xxmax));
   path cpath;
   cpath:=(xxmin,yymin)--(xxmax,yymin)--(xxmax,yymax)--(xxmin,yymax)-- 
cycle;
   p:=p cutbefore cpath;
   p:=reverse p;
   p:=p cutbefore cpath;
   p:=p xyscaled (uux,uuy);
   drawdblarrow p withcolor blue;
enddef;

\stopMPinclusions


\starttext

\startMPpage

%initialize function
vardef f(expr x)=
x*x
enddef;

%initialize window parameters
numeric xmin, xmax, xscl, ymin, ymax, yscl;
xmin:=-5;
xmax:=5;
xscl:=1;
ymin:=-5;
ymax:=5;
yscl:=1;

%initialize number of points
numeric num_points;
num_points:=100;

%initialize dimensions of image
numeric width, height;
width=3in;
height=3in;

%initialize scale
numeric ux, uy;
(xmax-xmin)*ux=width; (ymax-ymin)*uy=height;

%create the grid
create_grid(xmin,xmax,xscl,ymin,ymax,yscl,ux,uy);

%create the axes
create_axes(xmin,xmax,ymin,ymax,ux,uy)("$x$")("$y$");

%draw the function
drawf(xmin,xmax,ymin,ymax,ux,uy,num_points);

\stopMPpage

\stoptext

%%% Local Variables:
%%% mode: conTeXt-en
%%% End:

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2005-12-27 20:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-27 20:41 It works, passing text David Arnold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).