public class sphereRayTracer {
    private sphere sph = null;
    private double[] w = {0.0,0.0,0.0};
    private double[] v = {0.0,0.0,0.0};
    private double A = 0.0, B = 0.0, C = 0.0;
    private double vcx = 0.0, vcy = 0.0, vcz = 0.0;
    private double root1 = 0.0, root2 = 0.0;
    private double t = Double.POSITIVE_INFINITY;
    private double tn = t;
    private double dis = 0.0;
    private int numBounces = 0;

    public sphereRayTracer(sphere s,double[] vp,double[] dir) {
	sph = s;
	for(int i=0;i<3;i++) {
	    v[i] = vp[i];
	    w[i] = dir[i];
	}
	A = w[0]*w[0] + w[1]*w[1] + w[2]*w[2];
	vcx = v[0] - sph.getSphereCenter()[0];
	vcy = v[1] - sph.getSphereCenter()[1];
	vcz = v[2] - sph.getSphereCenter()[2];
	B = 2.0*vcx*w[0] + 2.0*vcy*w[1] + 2.0*vcz*w[2];
	C = vcx*vcx + vcy*vcy + vcz*vcz - sph.getSphereRadius()*sph.getSphereRadius();
	dis = B*B-4.0*A*C;
	
	if (dis < 0.0) {
	    //if (numBounces == 0)
	    sph = null;
	}
	else {
	    root1 = (-B+(double)Math.sqrt(dis))/(2.0*A);
	    root2 = (-B-(double)Math.sqrt(dis))/(2.0*A);
	    tn = (double)Math.min(root1,root2);
	    if ((tn > 0) && (tn < t))
		t = tn;
	    else 
		sph = null;
	}
    }
    public double getT() {
	return t;
    }
}

