Difference between revisions of "DynamicImages2"
(changed init_graph command to take the form: init_graph(xmin,ymin,xmax,ymax,options) (was: init_graph(-1,4,-1,4,options) which gave a division by zero error upon cut and paste)) |
|||
Line 29: | Line 29: | ||
<pre> |
<pre> |
||
# a graph object |
# a graph object |
||
− | $gr = init_graph(-1,-1,4,4,axes=>[0,0], |
+ | $gr = init_graph(-1,-1,4,4,axes=>[0,0], |
+ | grid=>[5,5]); |
||
# plot a function |
# plot a function |
||
− | add_functions($gr, |
+ | add_functions($gr, "4-3*e^(-x) for x in <-1,4> " . |
− | + | "using color:blue and weight:2"); |
|
# add a label |
# add a label |
||
$gr->lb( new Label(0.25,3.25,'(0,3)', |
$gr->lb( new Label(0.25,3.25,'(0,3)', |
||
− | 'black','center','center')); |
+ | 'black','center','center')); |
# a line |
# a line |
||
$gr->moveTo(-1,1); |
$gr->moveTo(-1,1); |
||
$gr->lineTo(4,1,'black'); |
$gr->lineTo(4,1,'black'); |
||
+ | # a thicker, blue line |
||
+ | # (only works for WeBWorK > 2.4) |
||
+ | $gr->lineTo(4,-1,'blue',2); |
||
+ | # a dashed blue line |
||
+ | # (only works for WeBWorK > 2.4) |
||
+ | $gr->lineTo(4,-1,'blue',1,'dashed'); |
||
+ | # a thicker red arrow from (0,0) to (4,1) |
||
+ | # (only works for WeBWorK > 2.4; |
||
+ | # also supports the 'dashed' option) |
||
+ | $gr->moveTo(0,0); |
||
+ | $gr->arrowTo(4,1,'red',2); |
||
# and a point |
# and a point |
||
Line 54: | Line 65: | ||
</p> |
</p> |
||
<p> |
<p> |
||
− | Lines are added by moving the graph "cursor" to a point and then drawing a line to a new point. |
+ | Lines are added by moving the graph "cursor" to a point and then drawing a line to a new point. Note that the "cursor" remains at the endpoint of the line after drawing is completed, so that any line drawn thereafter without an additional <code>moveTo</code> method call will start at that endpoint. |
</p> |
</p> |
||
<p> |
<p> |
Revision as of 08:51, 4 June 2009
Adding Labels, Lines and Points to Dynamic Graphs: PG Code Snippet
This code snippet shows the essential PG code to add labels, lines and points to dynamically generated graphs. 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.
PG problem file | Explanation |
---|---|
loadMacros("PGgraphmacros.pl"); |
We have no required additions to the tagging and description section of the file, and we obviously need to include |
# a graph object $gr = init_graph(-1,-1,4,4,axes=>[0,0], grid=>[5,5]); # plot a function add_functions($gr, "4-3*e^(-x) for x in <-1,4> " . "using color:blue and weight:2"); # add a label $gr->lb( new Label(0.25,3.25,'(0,3)', 'black','center','center')); # a line $gr->moveTo(-1,1); $gr->lineTo(4,1,'black'); # a thicker, blue line # (only works for WeBWorK > 2.4) $gr->lineTo(4,-1,'blue',2); # a dashed blue line # (only works for WeBWorK > 2.4) $gr->lineTo(4,-1,'blue',1,'dashed'); # a thicker red arrow from (0,0) to (4,1) # (only works for WeBWorK > 2.4; # also supports the 'dashed' option) $gr->moveTo(0,0); $gr->arrowTo(4,1,'red',2); # and a point $gr->stamps( closed_circle(0,3,'blue') ); |
In the initialization part of the file, we define a [dynamically generated graph|DynamicImages]. Then we can add labels, lines and points to the graph.
To add labels, we use the
Lines are added by moving the graph "cursor" to a point and then drawing a line to a new point. Note that the "cursor" remains at the endpoint of the line after drawing is completed, so that any line drawn thereafter without an additional
And we add points by using the |
BEGIN_TEXT \{ image( insertGraph($gr) ) \} END_TEXT |
And the graph is inserted in the text portion of the problem as we'd expect any static or dynamic image to be. |
Note that there is a more complete list of the plotting options available in a graph object in the CVS documentation for WWPlot and PGgraphmacros.