import java.awt.*;
import java.io.*;
import java.util.*;

class Ray {
    public static final double MAX_T = Double.MAX_VALUE;
    Vector3D origin;
    Vector3D direction;
    double t;
    Renderable object;
	double factor;

    public Ray(Vector3D eye, Vector3D dir, double f) {
        factor = f;
		origin = new Vector3D(eye);
        direction = Vector3D.normalize(dir);
    }

    public boolean trace(Vector objects) {
        Enumeration objList = objects.elements();
        t = MAX_T;
        object = null;
        while (objList.hasMoreElements()) {
            Renderable object = (Renderable) objList.nextElement();
            object.intersect(this);
        }
        return (object != null);
    }

	// blocked is just like trace but returns true right away if something is blocking
	// used by surface to determine shadows.
    public boolean blocked(Vector objects) {
        Enumeration objList = objects.elements();
        t = MAX_T;
        object = null;
        while ((object==null) && objList.hasMoreElements()) {
            Renderable object = (Renderable) objList.nextElement();
            object.intersect(this);
        }
        return (object != null);
    }

    // The following method is not strictly needed, and most likely
    // adds unnecessary overhead, but I prefered the syntax
    //
    //            ray.Shade(...)
    // to
    //            ray.object.Shade(ray, ...)
    //
    public final Color Shade(Vector lights, Vector objects, Color bgnd) {
        return object.Shade(this, lights, objects, bgnd);
    }

    public String toString() {
        return ("ray origin = "+origin+"  direction = "+direction+"  t = "+t);
    }
}