File: depart\math\maple\misc\shadein.mws Date: 9-dec-1999
Shading the region between two graphs
by Tim Feeman [timothy.feeman@villanova.edu]
[with collaborative hints from bob jantzen and klaus volpert]
> with(plots): with(plottools):
Warning, the name changecoords has been redefined
The following procedure shadein is designed to shade in the region between two curves over a specified interval. The inputs for the procedure are: the two functions (defined as procedures, not expressions), the desired interval on the x-axis (e.g., between their intersection points if desired, but any interval will work), and the color you want for the shading. You must use with(plottools): to use this procedure. If you wish to display it together with the original curves, you must use with(plot):
This procedure was created by analyzing the plottools ellipse command. It creates an upward sawtooth sequence of shaded triangles from the lower curve to the upper curve (if they are in this relation) , and then completes the shading with the complementary downward sawtooth sequence of triangles from the upper curve to the lower curve.
> shadein:=proc (f,g,a,c) local i, n, loptions; loptions := [args[5 .. nargs]]; if not hasoption(loptions,'numpoints','n','loptions') then n := 50 fi; ### WARNING: persistent store makes one-argument readlib obsolete loptions := convert(readlib(`plottools/local_2d`)(loptions), 'PLOToptions'); seq(polygon([ [evalf(a[1]+(a[2]-a[1])*i/n), evalf(f(a[1]+(a[2]-a[1])*i/n))], [evalf(a[1]+(a[2]-a[1])*(i+1)/n), evalf(f(a[1]+(a[2]-a[1])*(i+1)/n))], [evalf(a[1]+(a[2]-a[1])*(i+1)/n), evalf(g(a[1]+(a[2]-a[1])*(i+1)/n))]], color=c,style=patchnogrid,op(loptions)),i = 0 .. n-1), seq(polygon([[ evalf(a[1]+(a[2]-a[1])*i/n), evalf(g(a[1]+(a[2]-a[1])*i/n))], [evalf(a[1]+(a[2]-a[1])*(i+1)/n), evalf(g(a[1]+(a[2]-a[1])*(i+1)/n))], [evalf(a[1]+(a[2]-a[1])*i/n), evalf(f(a[1]+(a[2]-a[1])*i/n))]], color=c,style=patchnogrid,op(loptions)),i = 0 .. n-1) end;
> p:=plot({sin(x),cos(x)},x=0..Pi/2,color=black): display({p,shadein(sin,cos,[0,Pi/4],gray,numpoints=20)});
Huzzah!!!
> u:=x->x^2-1; v:=x->1-x^2; p1:=plot({u(x),v(x)},x=-2..2,color=black): solve(u(x)=v(x),x); pic:=shadein(u,v,[-1,1],gray): display({p1,pic});
>
Fantastic!!
the teeth
To show the guts of this procedure, we make the colors of the upper and lower teeth different:
> shadeinteeth:=proc (f,g,a,c) local i, n, loptions; loptions := [args[5 .. nargs]]; if not hasoption(loptions,'numpoints','n','loptions') then n := 50 fi; ### WARNING: persistent store makes one-argument readlib obsolete loptions := convert(readlib(`plottools/local_2d`)(loptions), 'PLOToptions'); seq(polygon([ [evalf(a[1]+(a[2]-a[1])*i/n), evalf(f(a[1]+(a[2]-a[1])*i/n))], [evalf(a[1]+(a[2]-a[1])*(i+1)/n), evalf(f(a[1]+(a[2]-a[1])*(i+1)/n))], [evalf(a[1]+(a[2]-a[1])*(i+1)/n), evalf(g(a[1]+(a[2]-a[1])*(i+1)/n))]], color=black,style=patchnogrid,op(loptions)),i = 0 .. n-1), seq(polygon([[ evalf(a[1]+(a[2]-a[1])*i/n), evalf(g(a[1]+(a[2]-a[1])*i/n))], [evalf(a[1]+(a[2]-a[1])*(i+1)/n), evalf(g(a[1]+(a[2]-a[1])*(i+1)/n))], [evalf(a[1]+(a[2]-a[1])*i/n), evalf(f(a[1]+(a[2]-a[1])*i/n))]], color=grey,style=patchnogrid,op(loptions)),i = 0 .. n-1) end;
> p:=plot({sin(x),cos(x)},x=0..Pi/2,color=black): display({p,shadeinteeth(sin,cos,[0,Pi/4],gray,numpoints=20)});