//<pre>

public class Sphere extends Shape3D {


	public Sphere(World w, int n) {
		
		super(w);
		
		int nv = (n+1)*(n+1);

		double newVertices[][] = new double[nv][6];
		int newFaces[][] = new int[n*n*2][3];

		int lastIndex = 0;
		double theta, phi;
		double x,y;
		
		for (int i = 0; i < n + 1; i++) {
			for (int j = 0; j < n + 1; j++) {
 				theta = (2* Math.PI * i / (float)n);
 				phi = (Math.PI * j / (float)n) - Math.PI/2;
 				newVertices[lastIndex  ][0] = Math.cos(phi)*Math.cos(theta);
 				newVertices[lastIndex  ][1] = Math.sin(phi);
 				newVertices[lastIndex++][2] = Math.sin(theta)*Math.cos(phi);
 				//System.out.println("i,j, theta, phi, x, y, z: " + i + ", " + j + ", " + theta + ", " + phi + ", " + newVertices[lastIndex -1][0] + ", " + newVertices[lastIndex -1][1]  + ", " + newVertices[lastIndex -1][2] );
				
			}
		}

		lastIndex = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				
				newFaces[lastIndex  ][0] = (i*(n+1)) + j;
				newFaces[lastIndex  ][1] = (i*(n+1)) + j + (n+1);
				newFaces[lastIndex++][2] = (i*(n+1)) + j         + 1;
				
				newFaces[lastIndex  ][0] = (i*(n+1)) + j + (n+1);
				newFaces[lastIndex  ][1] = (i*(n+1)) + j + (n+1) + 1;
				newFaces[lastIndex++][2] = (i*(n+1)) + j         + 1;
				
				
			}
		}

		super.vertices = newVertices;
		super.faces = newFaces;
	
		transformedVertices = new double[vertices.length][vertices[0].length];

		//this.print();
		
	}
	
}