PGgraphmacros -- in courseScripts directory

From WeBWorK

Jump to: navigation


NAME

        PGgraphmacros -- in courseScripts directory

SYNPOSIS

#use Fun;
#use Label;
#use Circle;
#use WWPlot;

DESCRIPTION

This collection of macros provides easy access to the facilities provided by the graph module WWPlot and the modules for objects which can be drawn on a graph: functions (Fun.pm) labels (Label.pm) and images. The only images implemented currently are open and closed circles (Circle) which can be used to mark graphs of functions defined on open and closed intervals.

These macros provide an easy ability to graph simple functions. More complicated projects may require direct access to the underlying modules. If these complicated projects are common then it may be desirable to create additional macros. (See numericalmacros.pl for one example.)

Other constructs

See PGbasicmacros for definitions of image and caption

init_graph

                $graphObject = init_graph(xmin,ymin,xmax,ymax,'ticks'=>[4,4],'axes'=>[0,0])
                options are
                        'grid' =>[8,8] or
                        # there are 8 evenly spaced lines intersecting the horizontal axis
                        'ticks'=>[8,8] and/or
                        # there are 8 ticks on the horizontal axis, 8 on the vertical
                        'axes' => [0,0]
                        # axes pass through the point (0,0) in real coordinates
                        'size' => [200,200]
                        # dimensions of the graph in pixels.
                        'pixels' =>[200,200]  # synonym for size

Creates a graph object with the default size 200 by 200 pixels. If you want axes or grids you need to specify them in options. But the default values can be selected for you.

plot_functions

        Usage:  ($f1, $f2, $f3) = plot_functions($graph, $f1, $f2, $f3);
        Synonym: add_functions($graph,$f1,$f2,$f3);

Where $f1 is a string of the form

        $f1 = qq! x^2 - 3*x + 45 for x in [0, 45) using color:red and weight:2!

The phrase translates as: formula for variable in interval using option-list. The option-list contains pairs of the form attribute:value. The default for color is "default_color" which is usually black. The default for the weight (pixel width) of the pen is 2 pixels.

The string_to_sub subroutine is used to translate the formula into a subroutine.

The functions in the list are installed in the graph object $graph and will appear when the graph object is next drawn.

insertGraph

        $filePath = insertGraph(graphObject);
                  returns a path to the file containing the graph image.

Note: insertGraph is defined in PGcore.pl, because it involves writing to the disk.

insertGraph(graphObject) writes a image file to the html/tmp/gif directory of the current course. The file name is obtained from the graphObject. Warnings are issued if errors occur while writing to the file.

The permissions and ownership of the file are controlled by $main::tmp_file_permission and $main::numericalGroupID.

Returns: A string containing the full path to the temporary file containing the image.

InsertGraph draws the object $graph, stores it in "${tempDirectory}gif/$imageName.gif (or .png)" where the $imageName is obtained from the graph object. ConvertPath and surePathToTmpFile are used to insure that the correct directory separators are used for the platform and that the necessary directories are created if they are not already present. The directory address to the file is the result.

The most common use of C,insertGraph> is

        TEXT(image(insertGraph($graph)) );

where image takes care of creating the proper URL for accessing the graph and for creating the HTML code to display the image.

Another common usage is:

        TEXT(htmlLink( alias(insertGraph($graph), "picture" ) ) );

which inserts the URL pointing to the picture.

alias() converts the directory address to a URL when serving HTML pages and insures that an eps file is generated when creating TeX code for downloading. (Image, automatically applies alias to its input in order to obtain the URL.)

See the documentation in PGcore.pl for the latest details.

'Circle' lables

        Usage: $circle_object = open_circle( $x_position, $y_position, $color );
                $circle_object2 = closed_circle( $x_position, $y_position, $color );

Creates a small open (resp. filled in or closed) circle for use as a stamp in marking graphs. For example

        $graph -> stamps($circle_object2); # puts a filled dot at $x_position, $y_position

Auxiliary macros

string_to_sub and my_math_constants

These are internal macros which govern the interpretation of equations.

        Usage: $string = my_math_constants($string)
               $subroutine_reference = my_string_to_sub($string)

my_math_constants interprets pi, e as mathematical constants 3.1415926... and 2.71828... respectively. (Case is important). The power operator ^ is replaced by ** to conform with perl constructs

string_to_sub converts a string defining a single perl arithmetic expression with independent variable $XVAR into a subroutine. The string is first filtered through my_math_macros. The resulting subroutine takes a single real number as input and produces a single output value.