// Spline class - Ingrid Brandt
  
class Spline
{
 protected:
   // set of control points for the spline.
   Point * points;
   
   // number of points in the spline.
   int numpoints;
   
   // degree of spline.
   int degree;
   
   double Min, Max;
   
 public:
   Spline ()
   {
      Max = 0;
      Min = 0;
      numpoints = 6;
      degree = 5;   
      
      points = new Point [numpoints];
      for (int i = 0; i < numpoints; i++)
      {
	 points[i].x = ((double) ((random () % 1000) - 500)) / 190.0;
	 points[i].y = ((double) ((random () % 1000) - 500)) / 190.0;
         if (points[i].x > Max) { Max = points[i].x; }
	 if (points[i].x < Min) { Min = points[i].x; }
      }
   }   
   
   double getMax()
   {
      return Max;
   }
   double getMin()
   {
      return Min;
   }
   int numberPoints ()
   {
      return numpoints;
   }
   
   int splineDegree ()
   {
      return degree;
   }
   
   void controlPoint (int i, double & x, double & y)
   {
      x = points[i].x;
      y = points[i].y;
   }
   
   void setControlPoint (int i, double x, double y)
   {
      points[i].x = x;
      points[i].y = y;
   }
   
   int fact(int p)
   {
      if (p<=0)
        return 1;
      else
        return (p*fact(p-1));
   }
   
   double B (int i, int p, double t)
   {
      double C = ((fact(p))/(fact(i)*fact(p-i)))*pow(t,i)*pow((1-t),(p-i));
      return C;    
   }
   
   void splineAt (double t, double & x, double & y)
   {
      x = 0.0;
      y = 0.0;
      for (int j = 0; j < numpoints; j++)
      {
         x += B (j, degree, t) * points[j].x;
         y += B (j, degree, t) * points[j].y;
      }
   }   
};     

