public class triangular_element1
{
public double a[],b[],c[];
public double dt;
public int P;
public double A;//Area
public double l[];
public double x[],y[];   //coordinates of data points m
public double xc,yc;     //center point m
public double N[],B[][]; // Shape factor and derivative
public double Ti[];      // Temperature nodes
public int n_point[];    // point numbers
public int n_element;    // element number

public triangular_element1(int n_elementi,int n_pointi[],double xi[],double yi[])
{dt=1.0;
 P=1;
 x=new double[3];
 y=new double[3];
 Ti=new double[3];
 l=new double[3];//0:lij,1:ljk,2:lik
 xc=0;yc=0;
 n_point=new int[3];
 n_element=n_elementi;
 for(int i=0;i<3;i++)
 { n_point[i]=n_pointi[i];
  x[i]=xi[i];y[i]=yi[i];
  xc+=x[i];yc+=y[i];
 }
 xc/=3.0;
 yc/=3.0;
 a=new double[3];
 b=new double[3];
 c=new double[3];
 N=new double[3];
 B=new double[2][3];
 a[0]=x[1]*y[2]-x[2]*y[1];
 a[1]=x[2]*y[0]-x[0]*y[2];
 a[2]=x[0]*y[1]-x[1]*y[0];
 b[0]=y[1]-y[2];
 b[1]=y[2]-y[0];
 b[2]=y[0]-y[1];
 c[0]=x[2]-x[1];
 c[1]=x[0]-x[2];
 c[2]=x[1]-x[0];
 A=0.5*((x[0]*y[1]-x[1]*y[0])+(x[2]*y[0]-x[0]*y[2])+(x[1]*y[2]-x[2]*y[1]));
 for(int i=0;i<3;i++)
 { B[0][i]=1.0/(2.0*A)*b[i];
   B[1][i]=1.0/(2.0*A)*c[i];	 
 }
 l[0]=Math.sqrt(b[2]*b[2]+c[2]*c[2]);
 l[1]=Math.sqrt(b[0]*b[0]+c[0]*c[0]);
 l[2]=Math.sqrt(b[1]*b[1]+c[1]*c[1]);
}
public triangular_element1(int n_elementi,int n_pointi[],double xi[],double yi[],double dti,int Pi)
{
 x=new double[3];
 y=new double[3];
 Ti=new double[3];
 l=new double[3];//0:lij,1:ljk,2:lik

 n_element=n_elementi;
 for(int i=0;i<3;i++)
 {n_point[i]=n_pointi[i];
  x[i]=xi[i];y[i]=yi[i];
  xc+=x[i];yc+=y[i];
 }
 xc/=3.0;
 yc/=3.0;
 a=new double[3];
 b=new double[3];
 c=new double[3];
 N=new double[3];
 B=new double[2][3];
 a[0]=x[1]*y[2]-x[2]*y[1];
 a[1]=x[2]*y[0]-x[0]*y[2];
 a[2]=x[0]*y[1]-x[1]*y[0];
 b[0]=y[1]-y[2];
 b[1]=y[2]-y[0];
 b[2]=y[0]-y[1];
 c[0]=x[2]-x[1];
 c[1]=x[0]-x[2];
 c[2]=x[1]-x[0];
 A=0.5*((x[0]*y[1]-x[1]*y[0])+(x[2]*y[0]-x[0]*y[2])+(x[1]*y[2]-x[2]*y[1]));
 for(int i=0;i<3;i++)
 { B[0][i]=1.0/(2.0*A)*b[i];
   B[1][i]=1.0/(2.0*A)*c[i];	 
 }
 l[0]=Math.sqrt(b[2]*b[2]+c[2]*c[2]);
 l[1]=Math.sqrt(b[0]*b[0]+c[0]*c[0]);
 l[2]=Math.sqrt(b[1]*b[1]+c[1]*c[1]);
}



public int setI(int cond,double L0,double L,double W0,double W)
{  
   int i=100;
	if(cond==0) //x=0
	{if(x[0]==L0 && x[1]==L0) i=0;
	 else if(x[1]==L0 && x[2]==L0) i=1;
	 else if(x[0]==L0 && x[2]==L0) i=2;
	 else i=-1;
	}
	else if(cond==1) //x=L
	{if(x[0]==L && x[1]==L) i=0;
	 else if(x[1]==L && x[2]==L) i=1;
	 else if(x[0]==L && x[2]==L) i=2;
	 else i=-1;
	}
	else if(cond==2) //y=0
	{if(y[0]==W0 && y[1]==W0) i=0;
	 else if(y[1]==W0 && y[2]==W0) i=1;
	 else if(y[0]==W0 && y[2]==W0) i=2;
	 else i=-1;
	}
	else if(cond==3) //y=W
	{if(y[0]==W && y[1]==W) i=0;
	 else if(y[1]==W && y[2]==W) i=1;
	 else if(y[0]==W && y[2]==W) i=2;
	 else i=-1;
	}
	return i;	
}


public void setN(double x,double y)
{for(int i=0;i<3;i++)
  {N[i]=1.0/(2.0*A)*(a[i]+b[i]*x+c[i]*y);}
}
public void setN(double z[])
{setN(z[0],z[1]);}

public void setT(double TT[])
{for(int i=0;i<3;i++)
  {Ti[i]=TT[i];}
}

public double[] getT()
{ return Ti;}

public double T(double x,double y)
{ //interpolation
	double TT=0;
 setN(x,y);
 for(int i=0;i<3;i++)
  {TT+=N[i]*Ti[i];}
   return TT;
}

public double T(double T[],double x,double y)
{ //set grid point temperatures and get interpolated value
	setT(T);
  setN(x,y);
  double TT=0;
 for(int i=0;i<3;i++)
  {TT+=N[i]*Ti[i];}
  return TT;
}
public double T(double T[],double z[])
{ return T(T,z[0],z[1]);}

}