# GraphTool

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

## Graph Tool

This example shows how to get student input in the form of a graph (a circle) by using interactive graphing tools.

PG problem file Explanation
```DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"parserGraphTool.pl"
);
TEXT(beginproblem());
```

Initialization: It is important to include the parseGraphTool.pl macro.

```\$h = non_zero_random(-5, 5);
\$k = non_zero_random(-5, 5);
\$r = random(1, 4);

\$circle_eq_lhs = Formula("(x-\$h)^2 + (y-\$k)^2")->reduce;

# This grader allows the student to graph the correct circle multiple times.  The idea is that
# the graph is graded based on appearance.  No matter how many times the student graphs the
# correct circle, the resulting graph appears the same.
\$gt = GraphTool("{circle, solid, (\$h, \$k), (\$h + \$r, \$k)}")->with(
bBox => [-11, 11, 11, -11],
cmpOptions => {
list_checker => sub
{
my (\$correct, \$student, \$ans, \$value) = @_;
return 0 if \$ans->{isPreview};

my \$score = 0;
my @errors;
my \$count = 1;

# Get the center and point that define the correct circle and compute the square of
my (\$cx, \$cy) = \$correct->[0]->extract(3)->value;
my (\$px, \$py) = \$correct->[0]->extract(4)->value;
my \$r_squared = (\$cx - \$px) ** 2 + (\$cy - \$py) ** 2;

my \$pointOnCircle = sub
{
my \$point = shift;
my (\$x, \$y) = \$point->value;
return (\$x - \$cx) ** 2 + (\$y - \$cy) ** 2  == \$r_squared;
};

for (@\$student)
{
my \$nth = Value::List->NameForNumber(\$count++);

\$score += 1, next
if (\$_->extract(1) eq \$correct->[0]->extract(1) &&
\$_->extract(2) eq \$correct->[0]->extract(2) &&
\$_->extract(3) == \$correct->[0]->extract(3) &&
\$pointOnCircle->(\$_->extract(4)));

push(@errors, "The \$nth object graphed is not a " . \$correct->[0]->extract(1)),
next if (\$_->extract(1) ne \$correct->[0]->extract(1));

push(@errors, "The \$nth object graphed should be a " . \$correct->[0]->extract(2) . " circle."),
next if (\$_->extract(2) ne \$correct->[0]->extract(2));

push(@errors, "The \$nth object graphed is incorrect.");
}

return (\$score, @errors);
}
}
);
```

Setup:

Notes: on using this and related Contexts.

```BEGIN_PGML

[@ image(insertGraph(\$graph_image), width => 300, tex_size => 1000) @]*

END_PGML
```

Main Text: This is how to insert the tikz image. Note the width and tex_size parameters can change the size of the image on the web and as hardcopy.

```ENDDOCUMENT();
```

This doesn't have a question, so we aren't checking an answer.