// Métodos de curva para creador. Versión 1.0. 24-04-01. Victor UVE.
// Subobjeto polinomioCúbico. Hereda de creador. Versión 1.0. 24-04-01. Victor UVE.
creador.instrumenta.prototype.slide=function(xf,yf,res,vel,kine,nextEv,noRun){
	kine=kine||0;
	this.setBezier(this.x,this.y,xf,yf,Math.round(this.x+(xf-this.x)/20*(10+kine)),Math.round(this.y+(yf-this.y)/20*(10+kine)),res,vel,nextEv,noRun);
}
creador.instrumenta.prototype.bezier=function(xf,yf,xbezier,ybezier,res,vel,nextEv,noRun){
	this.setBezier(this.x,this.y,xf,yf,xbezier,ybezier,res,vel,nextEv,noRun);
}
creador.instrumenta.prototype.setBezier=function(x0,y0,x2,y2,x1,y1,res,vel,nextEv,noRun){
	var x=[];
	var y=[];
	var a,b,c,d,t;
	for(var i=0;i<=res;i++){
		t=i/res;
		a=1-t;
		b=a*a;
		c=t*t;
		d=2*t*a;
		x[i]=Math.round(b*x0+d*x1+c*x2);
		y[i]=Math.round(b*y0+d*y1+c*y2);	
	}
	this.curve=[x,y,0,res,vel,nextEv];
	if(noRun==null||!noRun)this.runCurve();
}
creador.instrumenta.prototype.cubicBezier=function(x3,y3,x1,y1,x2,y2,res,vel,nextEv,noRun){
	var x0=this.x;
	var y0=this.y;
	var x=[];
	var y=[];
	var a,b,c,d,tt,t;
	for(var i=0;i<=res;i++){
		t=i/res;
		tt=1-t;
		a=tt*tt*tt;
		b=tt*tt*3*t;
		c=tt*3*t*t;
		d=t*t*t;
		x[i]=Math.round(a*x0+b*x1+c*x2+d*x3);
		y[i]=Math.round(a*y0+b*y1+c*y2+d*y3);
	}
	this.curve=[x,y,0,res,vel,nextEv];
	if(noRun==null||!noRun)this.runCurve();
}
creador.instrumenta.prototype.NCS=function(px,py,res,vel,nextEv,noRun){
	var np=px.length;
	var ni=np-1;
	var x=[];
	var y=[];
	if(np>1){
		var Dx=this.coeficientesNCS(ni,px);
		var Dy=this.coeficientesNCS(ni,py);
		var u;
		var ind=0;
		x[0]=Math.floor(Dx[0].solve(0));
		y[0]=Math.floor(Dy[0].solve(0));
		for(var i=0;i<ni;i++){
			for(var j=1;j<=res;j++){
				u=j/res;
				ind++;
				x[ind]=Math.floor(Dx[i].solve(u));
				y[ind]=Math.floor(Dy[i].solve(u));
			}
		}
		this.curve=[x,y,0,res*ni,vel,nextEv];
		if(noRun==null||!noRun)this.runCurve();
	}
}
creador.instrumenta.prototype.coeficientesNCS=function(n,p){
	var gamma=[];
	var delta=[];
	var D=[];
	gamma[0]=1/2;
	for(var i=1;i<n;i++)gamma[i]=1/(4-gamma[i-1]);
	gamma[n]=1/(2-gamma[n-1]);
	delta[0]=3*(p[1]-p[0])*gamma[0];
	for(var i=1;i<n;i++)delta[i]=(3*(p[i+1]-p[i-1])-delta[i-1])*gamma[i];
	delta[n]=(3*(p[n]-p[n-1])-delta[n-1])*gamma[n];
	D[n]=delta[n];
	for(var i=n-1;i>=0;i--)D[i]=delta[i]-gamma[i]*D[i+1];
    var C=[];
    for(var i=0;i<n;i++)C[i]=new creador.polinomioCubico(p[i],D[i],3*(p[i+1]-p[i])-2*D[i]-D[i+1],2*(p[i]-p[i+1])+D[i]+D[i+1]);
    return C;
}
creador.polinomioCubico=function(a,b,c,d){
	this.p=arguments;
}
creador.polinomioCubico.prototype.solve=function(u){
	return this.p[0]+u*(this.p[1]+u*(this.p[2]+u*this.p[3]));
}
creador.instrumenta.prototype.runCurve=function(){
	if(this.stoCurve)clearTimeout(this.stoCurve);
	var c=this.curve;
	var i=c[2];
	if(i<=c[3]){
		this.move(c[0][i],c[1][i]);
		this.curve[2]++;
		this.stoCurve=setTimeout(this.idObj+".runCurve()",c[4]);
	}else if(c[5])eval(c[5]);
}
creador.instrumenta.prototype.getCurve=function(){
	return this.curve;
}