import java.io.*; import java.lang.*; /** * Plane represents an infinite plane object * * @author Anthony Steed * @version 1.0 */ public class Plane extends Object { /** * Holds the normal that defines this plane */ public Vector Normal; /** * Holds the constant of the plane equation */ public double Constant; /** * Create a default plane */ public Plane() { SurfaceMaterial = new Material(); Normal = new Vector(); } /** * Create a plane with the given material and plane equation * * Note values are referenced not copied in the new object * @param n the material for the plane * @param v the vector that plane is normal to * @param c the constant of the plane equation */ public Plane(Material n, Vector v, double c) { SurfaceMaterial = n; Normal = v; Constant = c; } /** * Find the normal of an object at the given point on its surface. * * Note that for planes the normal is constant and a reference to * the internal vector is returned. * @param pt the surface point to find the normal at * @return a reference to the fixed normal of the plane */ public Vector normal(Point p) { return Normal; } /** * Find the intersection of the plane and a given ray. * * The return value is positive is the intersection is found and * this value gives the distance along the ray. Negative values * imply that the intersection was either not successful or the * intersection point was before the origin. This value can be used * with the pointAt method of the Ray class (@see Ray#pointAt) * * @param ray the ray to intersect with * @return a double value that gives the distance * along the ray. */ public double intersect(Ray ray) { Vector tmp; double d1,d2,t; tmp = new Vector (ray.Origin.X, ray.Origin.Y, ray.Origin.Z); /* Find angle between plane and direction of ray*/ d2 = Vector.dot(ray.Direction, Normal); /* Is ray parallel to plane */ if (Math.abs(d2) < 0.00000001) { return -1.0; } d1 = Vector.dot(tmp,Normal); t = (Constant - d1)/d2; return t; } /** * Read the plane from the given source * @param is the source to read from * @exception java.io.IOException * if the light can not be read * @exception java.io.NumberFormatException * if there a number format error is encountered */ public void read(SceneReader is) throws IOException, NumberFormatException { Normal.read(is); Constant = is.readDouble(); SurfaceMaterial.read(is); } /** * Write the plane to the given destination * @param os the destination to write to * @exception java.io.IOException * if the write fails. */ public void write(SceneWriter os) throws IOException { Normal.write(os); os.writeDouble(Constant); os.writeChar(' '); SurfaceMaterial.write(os); } /** * Print a human readable version of the plane definition to the * given destination * @param os the destination to write to * @exception java.io.IOException * if the write fails. */ public void print(SceneWriter os) throws IOException { os.writeString("Plane with normal: "); Normal.print(os); os.writeString(" and constant: "); os.writeDouble(Constant); os.writeString("\n"); SurfaceMaterial.print(os); os.writeString("\n"); } }