package Maths;

import java.util.Vector;

import Ui.*;


public class Bezier extends Polynom{

/** this class represents the Bezier curves and the methods assiociated with them**/

  public Bezier (Vector controlpoints, int degree){

	/* constructor */
	super(controlpoints, degree);

  }

  public Bezier (){

	/* constructor */
	super();

  }

 public Polynom Clone (){

	return  new Bezier();

 }

 protected double Alpha(int i, int j,double val,double []knots){

   /*defines the alpha used in the deBoor scheme */

    return val;

 };



   public int[][] GiveDrawPoints(){

    /* returns the drawing points, using the controlpoints in array format
       by calling upon the method Subdiv, which will return the drawing points


	output :  the array of drawing points

	Last change:  TMC  12 Jan 98    9:12 pm
     */

    int max =(int) ((Math.log(number_drawing_points))/(Math.log(2.0)));

	    if ( degree >= minimal_degree ){

		    int[][] temp = Double2Int(Subdiv(controlp,max));//call Subdiv, which will calculate the drawing points,
								    // and return them as double array

		    return	   temp;

	    }

	    else{

	        int temp[][] = new int[2][2];

		temp[0][0] = 0;
		temp[0][1] = 0;
		temp[1][0] = 0;
		temp[1][1] = 0;

		return temp;

	    };


    }


 public void Parameterise(){

       /* Searches a parameterisation for the curve,


        */

	knots = new double[2][2];

	knots[0][0] = 0; //the first knot has value 0, and multiplicity 1

	knots[1][0] = 1;

	knots[0][1] = 1;

	knots[1][1] = 1;



 }


    private double[][] Subdiv(double controlpoints[][], int depth){

	/* Calculates the drawing points by using subdivision. The method calls itself recursively to perform
	   subdivision on the given controlpoints, resulting in two sets of new controlpoints, 'left' and 'right',
	   which are then joined together into one array and which is returned to the higher level of recursion

	   input: the controlpoints, the depth in the recursion tree

	   ouput: the new 'controlpoints'/drawpoints
	    */

	if ( depth == 0 ){

		return controlpoints;

	}

	else{

		double left[][] = new double[dimension][controlpoints[0].length];

		double right[][] = new double[dimension][controlpoints[0].length];

		Subdivision(0.5, controlpoints, left, right);

		return Join(Subdiv(left,depth-1),Subdiv(right,depth-1));

	};

    }


   private double[][] Join(double left[][], double right[][]){

	/* joins two arrays */


	int leftsize = left[0].length;

	int rightsize= right[0].length;

	double joinmatr[][] = new double[dimension][leftsize+rightsize];


	for ( int i = 0; i < leftsize ; i++ ){

		for ( int j = 0; j < dimension ;j++ ){

		joinmatr[j][i] = left[j][i];

		}

	};

	for ( int i = 0; i < rightsize ; i++ ){

		for ( int j = 0; j < dimension ;j++ ){

		joinmatr[j][leftsize+i] = right[j][i];

		}

	};

	return joinmatr;

   }


 public boolean deg_change(int ch){

	/* elevates the degree of the curve, by calling the DegreeElevation method

	   input: the amount by which the degree should change
	   output: boolean that confirms that the change of degree was carried out/authorised

	 */
//System.out.println(ch);

	if ( ch > 0 && degree >0 ){//the degree change can only be a elevation, not a lowering and only
				   // when one has more than one controlpoint

                //calling the degree elevation algorithm & followed immediately by the call
		//on the method within the curve class that sets the new controlpoints in vector format
		//
		curve.VectoriseControlPoints(DegreeElevation(curve.GiveControlPoints()));

		curve.additional_multiplicity = 0;

		curve.CalculateDrawPoints();

		return true;

	}

	else{
		return false;

        }

 }



 private double[][] DegreeElevation(Vector controlpoints){

 /** Performs a degree elevation of the Bezier curve

     input: the vector of controlpoints

     output: the list of new controlpoints**/

   int max = controlpoints.size();

   double newcontrolp[][] = new double[2][max+1];

   double controlp[][] = new double[2][max];

   int cummulated = 0;

   for ( int i = 0; i < max ; i++ ){

   	ControlPoint pnt = ((ControlPoint)(controlpoints.elementAt(i)));

	controlp[0][i] = pnt.x;

	controlp[1][i] = pnt.y;

   };

   degree = max;

   newcontrolp[0][0] = controlp[0][0];

   newcontrolp[1][0] = controlp[1][0];

   for(int i = 1;i < max ; i++){

     newcontrolp[0][i] = (((float)(degree-i))/degree)*controlp[0][i]+((float)i)/degree*controlp[0][i-1];

     newcontrolp[1][i] = (((float)(degree-i))/degree)*controlp[1][i]+((float)i)/degree*controlp[1][i-1];

   };

   newcontrolp[0][degree] = controlp[0][degree-1];

   newcontrolp[1][degree] = controlp[1][degree-1];


   return newcontrolp;

 }; 
  

 public void Subdivision(double border,double cntrlp[][], double left[][], double right[][]){

 /**Performs a subdivision of the Bezier curve

    input:the parameter value border where the division must be initiated;

          the list of controlpoints.

    result to variables: one list with first the left curve and the the right curve**/

	 double temp[][] ;

	 double dbpoints[] = new double[degree+1];

	 for(int dim = 0; dim < dimension; dim++){

	  temp = deBoor(cntrlp[dim],border,knots[0]);

	  for(int i = 0; i <= degree; i++){

		  left[dim][i] = temp[i][i];

		  right[dim][degree-i] = temp[degree][i];

	  };

	 };

 };

 public double[][] Subdivision(double border){

 /**Performs a subdivision of the Bezier curve

    input:the parameter value border where the division must be initiated;

          the list of controlpoints.

    output: one list with first the left curve and the the right curve**/

	 double temp[][] ;

	 double cntrlp[][] = controlp;

	 int step = controlp[0].length;

	 double newcntrlp[][] = new double[dimension][step*2];

	 double dbpoints[] = new double[degree+1];

	 for(int dim = 0; dim < dimension; dim++){

	  temp = deBoor(cntrlp[dim],border,knots[0]);

	  for(int i = 0; i <= degree; i++){

		  newcntrlp[dim][i] = temp[i][i];

		  newcntrlp[dim][step+degree-i] = temp[degree][i];

	  };

	 };

	return newcntrlp;
 };






}
