Shaded Polygon Graph

Dynamically generated graph of a function

Complete Code

Download file: DynamicGraphPolygon.pg

POD for Macro Files

See Also

PG problem file

Explanation

DOCUMENT();

loadMacros('PGstandard.pl', 'PGML.pl', 'PGtikz.pl', 'PGcourse.pl');

Preamble

The dynamic graphs are generated with PGtikz.pl, so this is needed.

# The setup for each plot is the same, so we'll use a perl block for this.
$plot_setup = qq/
\tikzset{>={Stealth[scale=1.5]}}
\filldraw[
    draw=LightBlue,
    fill=white,
    rounded corners=10pt,
    thick,use as bounding box
] (-5,5) rectangle (5,-5);
\draw[lightgray, dashed] (-5,-5) grid (5,5);
\draw (-5,0) -- (5,0) node [below left] {\(x\)};
\foreach \x in {-4,...,-1,1,2,...,4} \draw(\x,-4.5) node {\(\x\)};
\draw (0,-5) -- (0,5) node [below right] {\(y\)};
\foreach \y in {-4,...,-1,1,2,...,4} \draw(-4.5,\y) node {\(\y\)};
/;

# The vertices of the triangle chosen randomly will be ($x0,$y0), ($x1,$y0)
# and ($x0,$y1).
$x0 = random(-3, -1);
$x1 = random(1,  3);
$y0 = random(-3, -1);
$y1 = random(1,  3);

$graph1 = createTikZImage();
$graph1->tikzLibraries('arrows.meta');
$graph1->BEGIN_TIKZ
$plot_setup;
\filldraw[very thick, fill=LightGreen, draw=DarkGreen, opacity=0.5] ($x0,$y0)
    -- ($x1,$y0) -- ($x0,$y1) -- cycle;
END_TIKZ

# A plot of 1+sqrt(x) and shade underneath the graph.

$graph2 = createTikZImage();
$graph2->tikzLibraries('arrows.meta');
$graph2->BEGIN_TIKZ
$plot_setup;

\filldraw[fill=LightBlue, opacity=0.5, draw=blue] (1,1)
    -- plot[domain=1:4, smooth] (\x,{1+sqrt(\x)})
    -- (4,0) -- (1,0) -- cycle;
\draw[very thick, DarkBlue] plot [domain=0:5, smooth] (\x,{1+sqrt(\x)});
END_TIKZ

# A circle with center ($x0,$y0)
$x = random(-2, 2);
$y = random(-2, 2);

$graph3 = createTikZImage();
$graph3->tikzLibraries('arrows.meta');
$graph3->BEGIN_TIKZ
$plot_setup;

\filldraw[very thick, fill=LightSalmon, opacity=0.5, draw=DarkOrange]
    circle[radius=3] ($x,$y);
END_TIKZ

Setup

See Dynamic Graph for basics of using tikz.

Since we make three plots with the same setup, all of the commands that are common to the graphs are defined in the same perl string.

Each of the plots uses the \filldraw command. Options for this are

  • fill: the color of the fill region
  • draw: the color of the boundary.
  • very thick: the thickness of the boundary
  • opacity: the opacity (between 0 and 1) of the fill region.

In the polygon and region under the curve, the draw method uses the verticies (or the plot command which uses the curve itself) and ends with cycle indicated that the region is closed.

The colors are defined in the LaTeX xcolor package

BEGIN_PGML

[@ image($graph1, width => 400) @]*

[@ image($graph2, width => 400) @]*

[@ image($graph3, width => 400) @]*

END_PGML

Statement

Note that the tikz graph in $graph1, $graph2 and $graph3 to be shown is placed in the image function and since this is a function, it must go in a [@ ... @]* block.

BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.