# /home/jaubrey/webwork//pg_TRUNK/lib/Fun.pm

### From WeBWorK

# NAME

Fun

# SYNPOSIS

use Carp; use GD; use WWPlot; use Fun; $fn = new Fun( rule_reference); $fn = new Fun( rule_reference , graph_reference); $fn = new Fun ( x_rule_ref, y_rule_ref ); $fn = new Fun ( x_rule_ref, y_rule_ref, graph_ref );

# DESCRIPTION

This module defines a parametric or non-parametric function object. The function object is designed to be inserted into a graph object defined by WWPlot.

The following functions are provided:

## new (non-parametric version)

**$fn = new Fun( rule_reference);**-
rule_reference is a reference to a subroutine which accepts a numerical value and returns a numerical value. The Fun object will draw the graph associated with this subroutine. For example: $rule = sub { my $x= shift; $x**2}; will produce a plot of the x squared. The new method returns a reference to the function object.

**$fn = new Fun( rule_reference , graph_reference);**-
The function is also placed into the printing queue of the graph object pointed to by graph_reference and the domain of the function object is set to the domain of the graph.

## new (parametric version)

**$fn = new Fun ( x_rule_ref, y_rule_ref );**-
A parametric function object is created where the subroutines refered to by x_rule_ref and y_rule_ref define the x and y outputs in terms of the input t.

**$fn = new Fun ( x_rule_ref, y_rule_ref, graph_ref );**-
This variant inserts the parametric function object into the graph object referred to by graph_ref. The domain of the function object is not adjusted. The domain's default value is (-1, 1).

## Properites

All of the properties are set using the construction $new_value = $fn->property($new_value) and read using $current_value = $fn->property()

**tstart, tstop, steps**-
The domain of the function is (tstart, tstop). steps is the number of subintervals used in graphing the function.

**color**-
The color used to draw the function is specified by a word such as 'orange' or 'yellow'.

`$fn-`

color('blue')> sets the drawing color to blue. The RGB values for the color are defined in the graph object in which the function is drawn. If the color, e.g. 'mauve', is not defined by the graph object then the function is drawn using the color 'default_color' which is always defined (and usually black). **x_rule**-
A reference to the subroutine used to calculate the x value of the graph. This is set to the identity function (x = t ) when using the function object in non-parametric mode.

**y_rule**-
A reference to the subroutine used to calculate the y value of the graph.

**weight**-
The width in pixels of the pen used to draw the graph. The pen is square.

## Actions which affect more than one property.

**rule**-
This defines a non-parametric function.

$fn->rule(sub {my $x =shift; $x**2;} ) is equivalent to $fn->x_rule(sub {my $x = shift; $x;}); $fn->y_rule(sub {my $x = shift; $x**2;); $fn->rule() returns the reference to the y_rule.

**domain**-
$array_ref = $fn->domain(-1,1) sets tstart to -1 and tstop to 1 and returns a reference to an array containing this pair of numbers.

**draw**-
$fn->

`draw($graph_ref)`

draws the function in the graph object pointed to by $graph_ref. If one of the points bounding a subinterval is undefined then that segment is not drawn. This usually does the "right thing" for functions which have simple singularities.The graph object must respond to the methods below. The draw call is mainly for internal use by the graph object. Most users will not call it directly.

**$graph_ref->{colors}**-
a hash containing the defined colors

**$graph_ref ->im**-
a GD image object

**$graph_ref->lineTo(x,y, color_number)**-
draw line to the point (x,y) from the current position using the specified color. To obtain the color number use a construction such as

`$color_number = $graph_ref-`

{colors}{'blue'};> **$graph_ref->lineTo(x,y,gdBrushed)**-
draw line to the point (x,y) using the pattern set by SetBrushed (see GD documentation)

**$graph_ref->moveTo(x,y)**-
set the current position to (x,y)