ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* function machine
@ 2005-12-28  6:10 David Arnold
  2005-12-28  8:58 ` Hans Hagen
  2005-12-28 16:57 ` Mojca Miklavec
  0 siblings, 2 replies; 4+ messages in thread
From: David Arnold @ 2005-12-28  6:10 UTC (permalink / raw)
  Cc: Jacob Prystowsky, Bruce Wagner

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:

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: function machine
  2005-12-28  6:10 function machine David Arnold
@ 2005-12-28  8:58 ` Hans Hagen
  2005-12-28 16:57 ` Mojca Miklavec
  1 sibling, 0 replies; 4+ messages in thread
From: Hans Hagen @ 2005-12-28  8:58 UTC (permalink / raw)


David Arnold wrote:

>
> \startMPinclusions
> ...
> \stopMPinclusions
>
you can consider putting that code into an environment file

easier to maintain

Hans

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: function machine
  2005-12-28  6:10 function machine David Arnold
  2005-12-28  8:58 ` Hans Hagen
@ 2005-12-28 16:57 ` Mojca Miklavec
  2005-12-28 18:41   ` David Arnold
  1 sibling, 1 reply; 4+ messages in thread
From: Mojca Miklavec @ 2005-12-28 16:57 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 923 bytes --]

Please, put this example on the wiki (together with some sample images
that can be created, with or without some cheating).

The community would be greatful for that.

> 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.

If I understand it properly (that you only want to draw the content
inside the square, while the function may be a circle or something
lying completely outside the region), you could also use something
like
    clip currentpicture to cpath;
although you may have to change the code that draws double arrow then.

Mojca

[-- Attachment #2: Type: text/plain, Size: 139 bytes --]

_______________________________________________
ntg-context mailing list
ntg-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/ntg-context

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: function machine
  2005-12-28 16:57 ` Mojca Miklavec
@ 2005-12-28 18:41   ` David Arnold
  0 siblings, 0 replies; 4+ messages in thread
From: David Arnold @ 2005-12-28 18:41 UTC (permalink / raw)


Mojca,

I've used clip currentpicture to cpath before, then saved the result,  
draw the grid, draw the axes, then draw the saved clipped picture.  
But, you're right, I can't use drawdblarrow then.

In our classes, the drawdblarrow is important, having the same  
meaning as the ellipsis in 1, 3, 5, 7, 9, ..., that is, "etc." In   
the case of a parabola opening upward, the arrow on the right half  
would indicate that the graph continues opening upward and to the  
right indefinitely. This is an important distinction from say a line  
segment that "stops" at one end, especially when asked to determine  
the domain and range of a function from its graph.

The clipping path is a huge convenience for an instructor crafting  
plots for an exam, activity, or lesson. Otherwise, the instructor  
would have to calculate the points where the graph leaves the  
bounding box (less painful if you use Matlab, Maple, or Mathematica,  
or a graphing calculator), a time consuming task.

I am not so concerned about a plot that might lie entirely outside  
the clipping window. That just requires, as on the graphing  
calculator, that one adjust the window (different xmin, xmax, xscl,  
ymin, ymax, and yscl). I just need to be able to clip the plot and  
retain the part that lies inside the clipping window, then use  
drawdblarrow on the part that remains.

My clip routine won't (cutbefore, reverse, cutbefore) won't work on  
something like y=sqrt(3-x) for two reasons:

1. A domain problem (easily solved by adjusting the drawfcn command  
to include two more parameters: beginx, endx.

2. The clip routine is not general enough to handle this situation.

So, there's room for improvement if you want to use this routine on  
irrational, rational, and logarithmic functions.


On Dec 28, 2005, at 8:57 AM, Mojca Miklavec wrote:

> Please, put this example on the wiki (together with some sample images
> that can be created, with or without some cheating).
>
> The community would be greatful for that.
>
>> 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.
>
> If I understand it properly (that you only want to draw the content
> inside the square, while the function may be a circle or something
> lying completely outside the region), you could also use something
> like
>     clip currentpicture to cpath;
> although you may have to change the code that draws double arrow then.
>
> Mojca
>
> _______________________________________________
> ntg-context mailing list
> ntg-context@ntg.nl
> http://www.ntg.nl/mailman/listinfo/ntg-context

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-12-28 18:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-28  6:10 function machine David Arnold
2005-12-28  8:58 ` Hans Hagen
2005-12-28 16:57 ` Mojca Miklavec
2005-12-28 18:41   ` 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).