<?php
// Copyright Andrew Gilbert
// This class does all the important stuff involving data. Its use will be in creating a table
// and drawing a graph.
class data
{
  var $mean;
  var $averagePeriod;
  var $squarePeriod;
  var $percentError;
  var $percentErrorSquare;
  var $totalError;
  var $actualError;
  var $centroidX;
  var $centroidY;
  var $actualRulerError;
  
  function getActualError()
  {
  $absolute = ($this->percentErrorSquare / 100);
  $this->actualError = $this->squarePeriod * $absolute;
  $this->actualError = round($this->actualError, 3);
  }
  
  function getActualRulerError()
  {
  $this->actualRulerError = $this->getTotalError(2);
  $this->actualRulerError = ($this->actualRulerError);
  }
  function getPercentError($i)
  {
  $percentError = (($this->getTotalError($i) / $this->mean) * 100);
  $this->percentError = round($percentError, 2);
  $this->percentErrorSquare = round((2 * $this->percentError) , 2);
  }
  
  function getTotalError($i)
  {
  $cal1 = $_SESSION['cal' . $i];
  $read1 = $_SESSION['read' . $i];
  $random1 = $_SESSION['random' . $i];
    // find biggest error:
    $max = $cal1;
    if ($read1 > $max)
    {
    $max = $read1;
    }
    if ($random1 > $max)
    {
    $max = $random1;
    }
    
    // check if it's dominant
    $count = 0;
    $findDominant = $max / 3;
    if ($cal1 <= $findDominant)
    {
    $count += 1;
    }
    if ($read1 <= $findDominant)
    {
    $count += 1;
    }
    if ($random1 <= $findDominant)
    {
    $count += 1;
    }
    
    // if there is a dominant error, return that, if not, calculate total error.
    if ($count == 2)
    {
    $this->totalError = $max;
    return $this->totalError;
    }
    else
    {
    $total = pow($cal1, 2) + pow($read1, 2) + pow($random1, 2);
    $this->totalError = sqrt($total);
    return $this->totalError;
    }
  
  }
  
  // gets the average period of rotation
  function getAveragePeriod()
  {
  $this->averagePeriod = ($this->mean / 10);
  }
  
  // gets the period squared
  function getSquarePeriod()
  {
  // averagePeriod ^ 2 (i.e. square period)
  $this->squarePeriod = pow($this->averagePeriod, 2);
  $this->squarePeriod = round($this->squarePeriod, 3);
  }
  
  // returns the mean of the number of values entered
  function getMean($values)
  {
  $values = explode(",", $values);
  $numValues = count($values);
  $value = 0;
    foreach ($values as $value)
    {
      $total += $value;    
    }
  $this->mean = round(($total / $numValues), 2);
  return $this->mean;
  }
  
  function centroidX($numRuns)
  {
    $i = 1;
    $total = 0;
    // amount to minus from total length to get size
    $minus = 0;
      while ($i <= $numRuns)
      {
      $total += $_SESSION['startLength'] - $minus;
      $minus += $_SESSION['increments'];
      $i++;
      }
    
    $total = $total / ($numRuns + 1);
    $this->centroidX = $total;   
  }
  

}
?>