<?php
class graph
{
function drawGrid($maxX, $maxY, $increments)
{
$maxX = round(($maxX * 800), 1) + 50;
$maxY = ($maxY *100) + 50;
$increments = $increments * 100;
$_SESSION['maxX'] = $maxX;
$_SESSION['maxY'] = $maxY;
$image = @imagecreate($maxX, $maxY);
$backColour = ImageColorAllocate($image, 255, 255, 255);
$gridColour = ImageColorAllocate($image, 230, 230, 250);
$axisColour = ImageColorAllocate($image, 0, 0, 0);
$i = 0;
$x = 1;
while ($i < $maxX)
{
ImageLine($image, $x, 1, $x, $maxY, $gridColour);
$x += 5;
$i++;
}
$r = 0;
$y = 1;
while ($r < $maxY)
{
ImageLine($image, 1, $y, $maxX, $y, $gridColour);
$y += 5;
$r++;
}
ImageLine($image, 1, $maxY-1, $maxX, $maxY-1, $axisColour);
ImageLine($image, 1, 1, 1, $maxY, $axisColour);
ImagePNG($image, 'back.png');
}
function addPoint($x, $y)
{
$y = round(($y * 100));
$y = $_SESSION['maxY'] - $y;
$x = round(($x * 800));
$image = imagecreatefrompng("back.png");
$pointColour = ImageColorAllocate($image, 0, 0, 0);
ImageFilledEllipse($image, $x, $y, 4, 4, $pointColour);
ImagePNG($image, 'back.png');
imagedestroy($image);
}
function errorBars($x, $y, $err1, $err2)
{
$y = round(($y * 100));
$y = $_SESSION['maxY'] - $y;
$x = round(($x * 800));
$err1 = $err1 * 100;
$err2 = $err2 * 800;
$y1 = $y + $err1;
$y2 = $y - $err1;
$x1 = $x + $err2;
$x2 = $x - $err2;
$image = imagecreatefrompng("back.png");
$errorColour = ImageColorAllocate($image, 0, 0, 255);
ImageLine($image, $x1, $y, $x2, $y, $errorColour);
ImageLine($image, $x, $y1, $x, $y2, $errorColour);
ImagePNG($image, 'back.png');
imagedestroy($image);
}
function addScale($numRuns)
{
$maxX = $_SESSION['maxX'];
$maxY = $_SESSION['maxY'];
$increments = $_SESSION['increments'];
$image = imagecreatefrompng("back.png");
$textColour = ImageColorAllocate($image, 0, 0, 0);
$i = 1;
$r = 1;
while ($i <= $maxX)
{
$num = $increments * $r;
$x = round(($num * 800));
$x = $x - 9;
$y = $maxY - 15;
imagestring($image, 3, $x, $y, "$num", $textColour);
$i += $increments * 800;
$r++;
}
$r = 1;
while ($r <= $numRuns)
{
$num = $r;
$x = 9;
$y = $maxY - ($r * 100);
imagestring($image, 3, $x, $y, "$num", $textColour);
$r++;
}
ImagePNG($image, 'back.png');
imagedestroy($image);
}
function addCentroid($x, $y)
{
$y = round(($y * 100));
$y = $_SESSION['maxY'] - $y;
$x = round(($x * 800));
$image = imagecreatefrompng("back.png");
$centroidColour = ImageColorAllocate($image, 255, 99, 71);
ImageFilledEllipse($image, $x, $y, 4, 4, $centroidColour);
ImagePNG($image, 'back.png');
imagedestroy($image);
}
function plotLine($x, $y, $move)
{
$y = round(($y * 100));
$y = $_SESSION['maxY'] - $y;
$x = round(($x * 800));
$_SESSION['move'] += $move;
$change = $_SESSION['move'];
$x1 = 0;
$y1 = $_SESSION['maxY'] + $change;
$diffX = $x - $x1;
$diffY = $y - $y1;
$x2 = $x + $diffX;
$y2 = $y + $diffY +1;
$image = imagecreatefrompng("back.png");
$lineColour = ImageColorAllocate($image, 0, 0, 0);
ImageLine($image, $x1, $y1, $x, $y, $lineColour);
ImageLine($image, $x, $y, $x2, $y2, $lineColour);
ImagePNG($image, 'graph.png');
imagedestroy($image);
}
}
?>