From WeBWorK
Jump to: navigation, search

Dynamic Graphic Images, with Filled Regions

This code snippet shows the essential PG code to check student answers that are equations. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Problem Techniques Index

PG problem file Explanation
# "PGnumericalmacros.pl", # might be useful


Initialization: We need to include the macro file PGgraphmacros.pl.

foreach my $i (0..2) {
  $gr[$i] = init_graph(-5,-5,5,5,grid=>[10,10],axes=>[0,0],pixels=>[400,400]);
  foreach my $j (1..4) {
    $gr[$i]->lb( new Label(-4.7,  $j, $j,'black','center','middle'));
    $gr[$i]->lb( new Label(-4.7, -$j,-$j,'black','center','middle'));
    $gr[$i]->lb( new Label(  $j,-4.7, $j,'black','center','middle'));
    $gr[$i]->lb( new Label( -$j,-4.7,-$j,'black','center','middle'));
  $gr[$i]->lb( new Label(4.7,0.2,'x','black','center','middle'));
  $gr[$i]->lb( new Label(0.2,4.7,'y','black','center','middle'));
  $gr[$i]->new_color("lightblue", 214,230,244); # RGB
  $gr[$i]->new_color("darkblue",  100,100,255);
  $gr[$i]->new_color("darkgreen",   0, 86, 34);
  $gr[$i]->new_color("lightred",  245,234,229); # light red-purple
  $gr[$i]->new_color("darkred",   159, 64, 16); # red-brown
  $gr[$i]->new_color("nicegreen",   0,110,  0);

#  Filled triangle with dark border
#  Note: we could fill in any polygon by adding more sides
$xmin = random(-3,-1,1);
$xmax = random(1,3,1);
$ymin = random(-3,-1,1);
$ymax = random(1,3,1);
$gr[0]->lineTo($xmax,$ymin,"darkgreen",2); # bottom edge
$gr[0]->lineTo($xmin,$ymax,"darkgreen",2); # hypotenuse
$gr[0]->lineTo($xmin,$ymin,"darkgreen",2); # left edge

#  Integral as area under the curve example
"sqrt(x)+1  for x in <0,5> using color:darkblue and weight:2");

#  A filled in circle
" sqrt(4-x^2) for x in <-2,2> using color:darkred and weight:2",
"-sqrt(4-x^2) for x in <-2,2> using color:darkred and weight:2"

Setup: To use the fillRegion feature, we must first create a region enclosed by one particular color (like darkgreen or darkblue in the examples), then choose a point inside that closed region and a color to fill with (like lightgreen or lightblue in the examples). The fill feature will expand from the chosen point until it comes to the color that bounds the region (like darkgreen or darkblue), and it will cross solid lines of other colors, such as black coordinate axes, to do so.

The examples to the left use the features provided by WWPlot.pm, which is part of the base code of WeBWorK. The perl code used to create these graphs, GD.pm, is the engine used by WWPlot.pm. The features provided by WWPlot.pm are very accessible and easy to use (for example, you specify everything in terms of coordinates on the graph, and WWPlot.pm translates all of your commands into pixel coordinates on the image file that is generated). Sometimes, though, you may want to access features provided by GD.pm directly or need a feature not currently provided by WWPlot.pm. Below, we give a few examples of how to access features of GD.pm directly via the ->im-> command, and how to convert from graph coordinates to pixel coordinates on the image file via the x-coordinate to pixel converter ii() and the y-coordinate to pixel converter jj().

#  filled rectangle with dark border
#  uses some macros directly from GD.pm
$gr2 = init_graph(-4,-4,4,4,grid=>[8,8],axes=>[0,0],pixels=>[300,300]);
$lightblue = $gr2->im->colorAllocate(148,201,255);
$darkblue  = $gr2->im->colorAllocate(100,100,255);
#  translate from graph coordinates to pixel coordinates
$xminpixel = $gr2->ii($xmin);
$xmaxpixel = $gr2->ii($xmax);
$yminpixel = $gr2->jj($ymin);
$ymaxpixel = $gr2->jj($ymax);
#  use filledRectangle and rectangle from GD.pm, accessed via ->im-> 

#  Graph
#  filled circle with dark border
#  uses some macros directly from GD.pm
$r = random(1,4,1); # radius

$gr3 = init_graph(-5,-5,5,5,grid=>[10,10],axes=>[0,0],pixels=>[400,400]);
foreach my $i (1..4) {
  $gr3->lb( new Label(-4.7,$i,$i,'black','center','middle'));
  $gr3->lb( new Label(-4.7,-$i,-$i,'black','center','middle'));
  $gr3->lb( new Label($i,-4.7,$i,'black','center','middle'));
  $gr3->lb( new Label(-$i,-4.7,-$i,'black','center','middle'));
$gr3->lb( new Label(4.7,0.2,'x','black','center','middle'));
$gr3->lb( new Label(0.2,4.7,'y','black','center','middle'));

$lightblue = $gr3->im->colorAllocate(148,201,255);
$darkblue  = $gr3->im->colorAllocate(100,100,255);
#  use arc() and fillToBorder() from GD.pm, accessed via ->im-> 
$cxpixel = $gr3->ii(0); # x-coordinate of center
$cypixel = $gr3->jj(0); # x-coordinate of center
$dxpixel = $gr3->ii($r) - $gr3->ii(-$r); # diameter of ellipse, x-direction
$dypixel = $gr3->jj($r) - $gr3->jj(-$r); # diameter of ellipse, y-direction
# 0 to 360 are degrees

\{ image( insertGraph($gr[0]), height=>400, width=>400, tex_size=>800 ) \}
\{ image( insertGraph($gr[1]), height=>400, width=>400, tex_size=>800 ) \}
\{ image( insertGraph($gr[2]), height=>400, width=>400, tex_size=>800 ) \}

Main Text: The problem text section of the file is as we'd expect.

$showPartialCorrectAnswers = 1;


Answer Evaluation: As is the answer.

Problem Techniques Index

follow us