Shaded Polygon Graph

Dynamically generated graph of a function

Complete Code

Download file: DynamicGraphPolygon.pg

POD for Macro Files

PG problem file

Explanation

DOCUMENT();

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

Preamble

The dynamic graphs are generated with PGtikz.pl, so load that macro.

$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

$graph1AltText =
    "A filled triangle with vertices at ($x0, $y0), ($x1, $y0), and ($x0, $y1).";

# 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,0) -- plot[domain = 1:4] (\x, {1 + sqrt(\x)}) -- (4,0) -- cycle;
\draw[very thick, DarkBlue]
    plot [samples = 100, domain = 0:5, smooth] (\x, {1 + sqrt(\x)});
END_TIKZ

$graph2AltText =
    'The graph of a curve that starts at (0, 1) and increases to the right, '
    . 'increasing sharply at first, and less so as it continues to the right. '
    . 'The region above the x-axis and below the curve between '
    . 'x = 1 and x = 4 is filled.';

# A circle of radius 3 centered at ($x, $y).
$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]
    ($x, $y) circle[radius = 3];
END_TIKZ

$graph3AltText = "A circle of radius 3 centered at ($x, $y).";

Setup

See Dynamic Graph for the basics of using TikZ.

Since three plots are created with the same setup, this common setup is defined in the Perl string $plot_setup.

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 vertices (or the plot command which uses the curve itself) and ends with cycle indicating that the region is closed.

The colors are defined in the LaTeX xcolor package

BEGIN_PGML
[![$graph1AltText]!]{$graph1}{400}

[![$graph2AltText]!]{$graph2}{400}

[![$graph3AltText]!]{$graph3}{400}
END_PGML

Statement

Note that the TikZ graphs in $graph1, $graph2 and $graph3 are inserted in the problem text via the PGML image syntax with their corresponding alternate texts for screen reader users.

BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.