<?php
// Copyright Andrew Gilbert
// Contains all the code for actually drawing the graph. 
class graph
{
  
  // draws a grid based on different factors. At the moment the maximum Y height is not dynamically created
  // but the rest is. Y Maximum point height is set at 5. Should be sufficient for period^2 vs time graphs.
  function drawGrid($maxX, $maxY, $increments)
  {
  // multiply to scale distances to number of pixels required.
  $maxX = round(($maxX * 800), 1) + 50;
  $maxY = ($maxY *100) + 50;
  $increments = $increments * 100;
  $_SESSION['maxX'] = $maxX;
  $_SESSION['maxY'] = $maxY;

  // create image
  $image = @imagecreate($maxX, $maxY);
  $backColour = ImageColorAllocate($image, 255, 255, 255);
  $gridColour = ImageColorAllocate($image, 230, 230, 250);
  $axisColour = ImageColorAllocate($image, 0, 0, 0);
  
  // generate verticle grid lines
  $i = 0;
  $x = 1;
    while ($i < $maxX)
    {
    ImageLine($image, $x, 1, $x, $maxY, $gridColour);
    $x += 5;
    $i++;
    }
  
  // generate horizontal grid lines
  $r = 0;
  $y = 1;    
    while ($r < $maxY)
    {
    ImageLine($image, 1, $y, $maxX, $y, $gridColour);
    $y += 5;
    $r++;
    }
    
  // draw x-axis line:
  ImageLine($image, 1, $maxY-1, $maxX, $maxY-1, $axisColour);
  // draw y-axis line:
  ImageLine($image, 1, 1, 1, $maxY, $axisColour);
   
  // output/save image.
  ImagePNG($image, 'back.png');
  
  }
  
  // adds points to graph. Basically draws an ellipse with the centre at the point's 
  // coordinates in pixels
  function addPoint($x, $y)
  {
  // scale up coordinates and invert them to go onto graph properly
  $y = round(($y * 100));
  $y = $_SESSION['maxY'] - $y; 
  $x = round(($x * 800));
  
  //load background image
  $image = imagecreatefrompng("back.png");
  // colour of plot points
  $pointColour = ImageColorAllocate($image, 0, 0, 0);
  // position of plot points
  ImageFilledEllipse($image, $x, $y, 4, 4, $pointColour);
  
  // recalculate length left so that next measurement has decreased
    
  ImagePNG($image, 'back.png');
  imagedestroy($image);
  }
  
  // function for drawing error bars
  function errorBars($x, $y, $err1, $err2)
  {
  // scale up points
  $y = round(($y * 100));
  $y = $_SESSION['maxY'] - $y; 
  $x = round(($x * 800));
  
  // scale up error lengths
  $err1 = $err1 * 100;
  $err2 = $err2 * 800;
  
  $y1 = $y + $err1;
  $y2 = $y - $err1;
  $x1 = $x + $err2;
  $x2 = $x - $err2;
  
  //load background image
  $image = imagecreatefrompng("back.png");
  // colour of plot points
  $errorColour = ImageColorAllocate($image, 0, 0, 255);
  // draw error bars:
  ImageLine($image, $x1, $y, $x2, $y, $errorColour);
  ImageLine($image, $x, $y1, $x, $y2, $errorColour);
  
  // recalculate length left so that next measurement has decreased
    
  ImagePNG($image, 'back.png');
  imagedestroy($image);
  
  }
  
  // adds the scale to the image
  function addScale($numRuns)
  {
  $maxX = $_SESSION['maxX'];
  $maxY = $_SESSION['maxY'];
  $increments = $_SESSION['increments'];
    
  //load background image
  $image = imagecreatefrompng("back.png");
  // colour of text
  $textColour = ImageColorAllocate($image, 0, 0, 0);
  // draw scale:
  // add x-units
  $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 plots the centroid point
  function addCentroid($x, $y)
  {
    // scale up coordinates and invert them to go onto graph properly
  $y = round(($y * 100));
  $y = $_SESSION['maxY'] - $y; 
  $x = round(($x * 800));
    
  //load background image
  $image = imagecreatefrompng("back.png");
  // colour of plot points
  $centroidColour = ImageColorAllocate($image, 255, 99, 71);
  // position of plot points
  ImageFilledEllipse($image, $x, $y, 4, 4, $centroidColour);
  
  // recalculate length left so that next measurement has decreased
   
  ImagePNG($image, 'back.png');
  
  imagedestroy($image);
  }
  
  // draw the line based on x and y coordinates of centroid and distance moved up or down
  // y axis (i.e. steepness)
  function plotLine($x, $y, $move)
  {
  // $x and $y are centroid coordinates that must be scaled up
  $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;

  //load background image
  $image = imagecreatefrompng("back.png");
  // colour of plot points
  $lineColour = ImageColorAllocate($image, 0, 0, 0);
  // draw error bars:
  ImageLine($image, $x1, $y1, $x, $y, $lineColour);
  ImageLine($image, $x, $y, $x2, $y2, $lineColour);
  
  
  
  // recalculate length left so that next measurement has decreased
    
  ImagePNG($image, 'graph.png');
  imagedestroy($image);
  
  }
   
}

?>