import java.awt.*;
import java.util.*;
import java.lang.Math;

public class ParamTorus extends ParamSurface //Sphere is the subclass
{
	double dTheta = 0, dPhi = 0;
	double R, r;
	double dEpsilon;
	int iEpsInv;
	static double dCenter[] = new double[4];
	double Torus3D[][][];

	public ParamTorus(){}

	public ParamTorus(double Rnew, double rnew, double epsilon,
			int iHeight, int iWidth)
	{
		R = Rnew;
		r = rnew;
		dEpsilon = epsilon;
		iEpsInv = (int)(1/dEpsilon);
		
		setBounds(iHeight, iWidth, dEpsilon);
		Torus3D  = new double[iEpsInv+1][iEpsInv][4];
		int i=0, j=0;		
		for (double v = 0; v < 1+dEpsilon; v += dEpsilon)
		{
			for (double u = 0; u < 1; u += dEpsilon)
			{
				Torus3D[i][j][0] = x(u,v);
				Torus3D[i][j][1] = y(u,v);
				Torus3D[i][j][2] = z(u,v);
				Torus3D[i][j][3] = 1;
				j++;
			}
			i++;
			j=0;
		}

		dCenter[0] = 0.0;
		dCenter[1] = 0.0;
		dCenter[2] = 0.0;
		dCenter[3] = 1;
	}		

	public double x(double u, double v)
	{
		dTheta = 2 * Math.PI * u;
		dPhi = 2 * Math.PI * v;
		
		return Math.cos(dTheta) * ( R + r * Math.cos(dPhi));
	}	

	public double y(double u, double v)
	{
		dPhi = 2 * Math.PI * v;
		
		return r * Math.sin(dPhi);
	}	

	public double z(double u, double v)
	{
		dTheta = 2 * Math.PI * u;
		dPhi = 2 * Math.PI * v;
		
		return -1 * Math.sin(dTheta) * (R + r * Math.cos(dPhi));
	}

	public double[][][] getShape()
	{
		return Torus3D;
	}
	
	public void setShape(double newTorus[][][])
	{
		for (int i=0; i<iEpsInv+1;i++)
			for (int j=0; j<iEpsInv;j++)
				Torus3D[i][j] = newTorus[i][j];
	}
	
	public double[] getCenter()
	{
		return dCenter;
	}
	
	public double getRadius()
	{
		return R;
	}
	
	public void setCenter(double newCenter[])
	{
		for (int i=0; i<4; i++)
			dCenter[i] = newCenter[i];
	}
}
