import java.io.*; import java.lang.*; /** * Point stores and manipulates xyz triples representing 3 space * points. * * @author Anthony Steed * @version 1.0 */ public class Point { /** * Holds the X component */ public double X; /** * Holds the Y component */ public double Y; /** * Holds the Z component */ public double Z; /** * Create a new point and set it to zero */ public Point() { X=0.0;Y=0.0;Z=0.0; } /** * Create a new point with the given x,y and z values * @param x the x value * @param y the y value * @param z the z value */ public Point(double x, double y, double z) { X=x; Y=y; Z=z; } /** * Set this point to the given x,y and z values * @param x the x value * @param y the y value * @param z the z value */ public void set(double x, double y, double z) { X=x; Y=y; Z=z; } /** * Create a new point by copying an existing point * @param v the point to copy */ public Point(Point p) { X=p.X; Y=p.Y; Z=p.Z; } /** * Copy an existing point to this point * @param v the point to copy */ public void copy(Point p) { X=p.X; Y=p.Y; Z=p.Z; } /** * Add the given offset vector to this point * @param v the vector to add */ public void add(Vector v) { X += v.X; Y += v.Y; Z += v.Z; } /** * Test if two points are equal * @param v the vector to test against */ public boolean equals(Point p) { return ((X==p.X) && (Y==p.Y) && (Z==p.Z)); } /** * Static function create a new point by adding a vector to a point. * @param p the original point * @param v the vector offset * @return the new point */ public static Point add(Point p, Vector v) { return new Point(p.X+v.X,p.Y+v.Y,p.Z+v.Z); } /** * Static function that write addition of a point by adding a * vector to a given destiation point * @param pd the destination point * @param p the original point * @param v the vector offset */ public static void add(Point pd, Point p, Vector v) { pd.X = p.X + v.X; pd.Y = p.Y + v.Y; pd.Z = p.Z + v.Z; } /** * Read the point 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 { X = is.readDouble(); Y = is.readDouble(); Z = is.readDouble(); } /** * Write the point 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 { os.writeDouble(X); os.writeChar(' '); os.writeDouble(Y); os.writeChar(' '); os.writeDouble(Z); os.writeChar(' '); } /** * Print a human readable version of the point 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.writeChar('['); os.writeDouble(X); os.writeChar(','); os.writeDouble(Y); os.writeChar(','); os.writeDouble(Z); os.writeChar(']'); } public String toString() { return "[" + X + ", " + Y + ", " + Z + "]"; } }