<?php

class Plot 
{
  private $aCoords;
  function __construct(&$aCoords) 
  {
    $this->aCoords = &$aCoords;
  }

  public function drawLine($vImage, $vColor, $iPosX = 0, $imaxX = false) 
  {
    $maxX = $imaxX;
    if ($imaxX === false) $maxX = imagesx($vImage);

    reset($this->aCoords);
    list($iPrevX, $iPrevY) = each($this->aCoords);

    while (list ($x, $y) = each($this->aCoords)) 
    {
      $laCouleur = null;
      if (!is_array($vColor)) $laCouleur = $vColor;
      else
      {
        $s = count($vColor) - 1;
        $laCouleur = $vColor[$s]->color;
        $pct = $x / $maxX;
        while(($s>0)&&($pct < $vColor[$s]->pct))
        {
          $s -= 1;
          $laCouleur = $vColor[$s]->color;
        }
      }
      imageline($vImage, round($iPrevX), round($iPrevY), round($x), round($y), $laCouleur);
      $iPrevX = $x;
      $iPrevY = $y;
    }
  }

  public function drawDots($vImage, $vColor, $iPosX = 0, $iPosY = false, $iDotSize = 1) {
    if ($iPosY === false)
      $iPosY = imagesy($vImage);
    $vBorderColor = imagecolorallocate($vImage, 0, 0, 0);
    foreach ($this->aCoords as $x => $y) {
      imagefilledellipse($vImage, $iPosX + round($x), $iPosY - round($y), $iDotSize, $iDotSize, $vColor);
      imageellipse($vImage, $iPosX + round($x), $iPosY - round($y), $iDotSize, $iDotSize, $vBorderColor);
    }
  }

  public function drawAxis($vImage, $vColor, $iPosX = 0, $iPosY = false) {
    if ($iPosY === false) $iPosY = imagesy($vImage);
    $vImageWidth = imagesx($vImage);
    imageline($vImage, $iPosX, $iPosY, $iPosX, 0, $vColor);
    imageline($vImage, $iPosX, $iPosY, $vImageWidth, $iPosY, $vColor);
    imagefilledpolygon($vImage, array($iPosX, 0, $iPosX - 3, 5, $iPosX + 3, 5), 3, $vColor);
    imagefilledpolygon($vImage, array($vImageWidth, $iPosY, $vImageWidth - 5, $iPosY - 3, $vImageWidth - 5, $iPosY + 3), 3, $vColor);
  }
}

class CubicSplines 
{
  protected $aCoords;
  protected $aCrdX;
  protected $aCrdY;
  protected $aSplines = array();
  protected $iMinX;
  protected $iMaxX;
  protected $iStep;
  protected function prepareCoords(&$aCoords, $iStep, $iMinX = -1, $iMaxX = -1) {
    $this->aCrdX = array();
    $this->aCrdY = array();
    $this->aCoords = array();
    ksort($aCoords);
    foreach ($aCoords as $x => $y) {
      $this->aCrdX[] = $x;
      $this->aCrdY[] = $y;
    }
    $this->iMinX = $iMinX;
    $this->iMaxX = $iMaxX;
    if ($this->iMinX == -1)
      $this->iMinX = min($this->aCrdX);
    if ($this->iMaxX == -1)
      $this->iMaxX = max($this->aCrdX);
    $this->iStep = $iStep;
  }
  public function setInitCoords(&$aCoords, $iStep = 1, $iMinX = -1, $iMaxX = -1) {
    $this->aSplines = array();
    if (count($aCoords) < 4) {
      return false;
    }
    $this->prepareCoords($aCoords, $iStep, $iMinX, $iMaxX);
    $this->buildSpline($this->aCrdX, $this->aCrdY, count($this->aCrdX));
  }
  public function processCoords() {
    for ($x = $this->iMinX; $x <= $this->iMaxX; $x += $this->iStep) {
      $this->aCoords[$x] = $this->funcInterp($x);
    }
    return $this->aCoords;
  }
  private function buildSpline($x, $y, $n) {
    for ($i = 0; $i < $n; ++$i) {
      $this->aSplines[$i]['x'] = $x[$i];
      $this->aSplines[$i]['a'] = $y[$i];
    }
    $this->aSplines[0]['c'] = $this->aSplines[$n - 1]['c'] = 0;
    $alpha[0] = $beta[0] = 0;
    for ($i = 1; $i < $n - 1; ++$i) {
      $h_i = $x[$i] - $x[$i - 1];
      $h_i1 = $x[$i + 1] - $x[$i];
      $A = $h_i;
      $C = 2.0 * ($h_i + $h_i1);
      $B = $h_i1;
      $F = 6.0 * (($y[$i + 1] - $y[$i]) / $h_i1 - ($y[$i] - $y[$i - 1]) / $h_i);
      $z = ($A * $alpha[$i - 1] + $C);
      $alpha[$i] = - $B / $z;
      $beta[$i] = ($F - $A * $beta[$i - 1]) / $z;
    }
    for ($i = $n - 2; $i > 0; --$i) {
      $this->aSplines[$i]['c'] = $alpha[$i] * $this->aSplines[$i + 1]['c'] + $beta[$i];
    }
    for ($i = $n - 1; $i > 0; --$i) {
      $h_i = $x[$i] - $x[$i - 1];
      $this->aSplines[$i]['d'] = ($this->aSplines[$i]['c'] - $this->aSplines[$i - 1]['c']) / $h_i;
      $this->aSplines[$i]['b'] = $h_i * (2.0 * $this->aSplines[$i]['c'] + $this->aSplines[$i - 1]['c']) / 6.0 + ($y[$i] - $y[$i - 1]) / $h_i;
    }
  }
  private function funcInterp($x) {
    $n = count($this->aSplines);
    if ($x <= $this->aSplines[0]['x'])  {
      $s = $this->aSplines[1];
    } else {
      if ($x >= $this->aSplines[$n - 1]['x']) {
        $s = $this->aSplines[$n - 1];
      } else {
        $i = 0;
        $j = $n - 1;
        while ($i + 1 < $j) {
          $k = $i + ($j - $i) / 2;
          if ($x <= $this->aSplines[$k]['x']) {
            $j = $k;
          } else {
            $i = $k;
          }
        }
        $s = $this->aSplines[$j];
      }
    }
    $dx = ($x - $s['x']);
    return $s['a'] + ($s['b'] + ($s['c'] / 2.0 + $s['d'] * $dx / 6.0) * $dx) * $dx;
  }
}

?>
