/*===============================================================*/ /* DOTS.JAVA */ /*===============================================================*/ /** *
 * Dots.java: A data class for 3D wire frame objects.  This
 *               class holds two arrays for representing points
 *               in three dimensions: a world coordinate scheme,
 *               and a transformed scheme.  A third array holds
 *               the indices of connected points, allowing for
 *               drawing of 3D objects.
 *
 *            This class also contains a simple method to rotate
 *               the points, given three angles.
 *
 *            The class implements the Serializable interface
 *               in order to support object serialization.
 *
 *
 * Revisions:  1.0  June 13, 1998,
 *                  Created class Dots.java
 *
 *
 * 
* * @author David Dagon * @version Version 1.0, June 13, 1998 */ /*===============================================================*/ import java.io.Serializable; class Dots implements Serializable { /*===============================================================*/ /* INSTANCE VARIABLES */ /*===============================================================*/ private p1 parent; protected double xs[], // original ys[], // coords. zs[], xt[], // transformed yt[], // coords. zt[]; protected int lines[][]; // array holding list // of connected points // first element is // sized to 'two'. protected int pts, // total points lns, // line count radius; // for future // use . . . private static int DEFAULT_PTS = 10; private boolean DEBUG; // capitalized // out of a // sense of // tradition /*===============================================================*/ /* CONSTRUCTORS */ /*===============================================================*/ /** * * Default Constructor * * Precondition: The use has called the default constructor * * Postcondition: The program is halted, after an error message * is printed * */ public Dots() { System.err.println ("Please don't invoke default Dots constructor"); System.exit(0); } /*===============================================================*/ /** * * Constructor -- p1 argument * * Precondition: The user has passed a reference to the 'p1' * class in the constructor call * * Postcondition: The constructor call is chained; debuggin * is presumed true. * * @param p1 parent -- the reference to the class creating * an instance of Dots.java */ public Dots (p1 parent) { this (parent, true); } /*===============================================================*/ /** * * Constructor -- p1, boolean argument * * Precondition: The user has passed a reference to the 'p1' * class in the constructor call, and specified a state * for the debugging boolean. * * Postcondition: The constructor is chained; the default * number of points is used. * * @param p1 parent -- the reference to the class creating * an instance of Dots.java * * @param boolean DEBUG -- debugging or not * */ public Dots (p1 parent, boolean DEBUG) { this (parent, DEBUG, DEFAULT_PTS); } /*===============================================================*/ /** * * Constructor -- p1, boolean, int argument * * Precondition: The user has passed a reference to the 'p1' * class in the constructor call, specified a state * for the debugging boolean, and the number of points * * Postcondition: The parameters are stored as local * variables, and the instance arrays are initialized * to the size of the number of points * * @param p1 parent -- the reference to the class creating * an instance of Dots.java * * @param boolean DEBUG -- debugging or not * * @param int pts -- the number of points to be tracked in * this instance * * @see this#initializeArrays() -- called to instantiate the * the array objects. * * */ public Dots (p1 parent, boolean DEBUG, int pts) { this.parent=parent; this.DEBUG=DEBUG; this.pts = pts; initializeArrays(pts); } /*===============================================================*/ /* MODIFIER METHODS */ /*===============================================================*/ /* * rotateEm(double, double, double) -- rotate the points * * @param double a -- the x angle of rotation * * @param double b -- the y angle of rotation * * @param double c -- the z angle of rotation * */ public void rotateEm(double a, double b, double c) { for (int i=0; i< pts; i++) { double Xa, Ya, Za, Xb, Yb, Zb; Xa=xs[i]*Math.cos(a)-ys[i]*Math.sin(a); Ya=xs[i]*Math.sin(a)+ys[i]*Math.cos(a); Za = zs[i]; Xb=Xa; Yb = Ya*Math.cos(b) - Za*Math.sin(b); Zb = Ya*Math.sin(b) + Za*Math.cos(b); xt[i]=Xb*Math.cos(c) + Zb *Math.sin(c); yt[i]=Yb; zt[i]= -Xb*Math.sin(c) + Zb*Math.cos(c); } } /*===============================================================*/ /** * * initializeArray() -- set up the array objects * * Precondition: The number of points has been specified * * Postcondition: The arrays are initialized to the size of 'pts' * * @param int pts -- the number of points in this class * */ public void initializeArrays(int pts) { xs=new double[pts]; ys=new double[pts]; zs=new double[pts]; xt=new double[pts]; yt=new double[pts]; zt=new double[pts]; // set up array of lines lines = new int[2][]; } /*===============================================================*/ /** * * setX -- set the array of X coordinates * * Precondition: an array of doubles has been passed in * * Postcondition: the array of doubles becomes the x coord array * * @param xs -- the untransformed array of x coordinates * */ public void setX(double[] xs) { this.xs=xs; } /*===============================================================*/ /** * * setY -- set the array of Y coordinates * * Precondition: an array of doubles has been passed in * * Postcondition: the array of doubles becomes the y coord array * * @param ys -- the untransformed array of y coordinates * */ public void setY(double[] ys) { this.ys=ys; } /*===============================================================*/ /** * * setZ -- set the array of Z coordinates * * Precondition: an array of doubles has been passed in * * Postcondition: the array of doubles becomes the z coord array * * @param zs -- the untransformed array of z coordinates * */ public void setZ(double[] zs) { this.zs=zs; } /*===============================================================*/ /** * * setRadius -- sets the radius of any drawn point * * Precondition: an integer has been passed in * * Postcondition: the radius used by all points is set to * the parameter * * @param radius -- the radius of all points * */ public void setRadius(int radius) { this.radius = radius; } /*===============================================================*/ /** * * setLines -- set the array of X lines * * Precondition: an array of lines has been passed in, holding * the index value of connected points in the x y and z * arrays. * * Postcondition: the array of lines becomes the new line * array * * @param lines -- the array of lines * */ public void setLines(int[][] lines) { this.lines = lines; } /*===============================================================*/ /** * * setPointCount -- set the number of points * * Precondition: the number of points has changed * * Postcondition: the number of points is stored as an * instance variable of this class * * @param pts -- the number of points * */ public void setPointCount(int pts) { this.pts = pts; } /*===============================================================*/ /* ACCESSOR METHODS */ /*===============================================================*/ /** * * toString -- return a String representation of the data held * by this instance. * * Precondition: a call to toString() * * Postcondition: a formatted String representation of the * data class is created * * @return String ret -- the String representation * */ public String toString() { String ret=""; for (int i=0; i "+xt[i]+"\n" + "y["+i+"]: "+ys[i]+" --> "+yt[i]+"\n" + "z["+i+"]: "+zs[i]+" --> "+zt[i]+"\n\n"; } ret+="\nLines:\n=============\n"; if (lines == null) return ret + "None"; for (int i=0; i< lns; i++) { ret+=lines[i][0] + " <--> " + lines[i][1] + "\n"; } return ret; } /*===============================================================*/ /** * * getPointString(int i) -- get a String representation of * a given point * * Precondition: A point exists at index i * * Postcondition: The point at index i is returned as a String * * @param i -- the index of the point * * @return String -- a String representation of the point * */ public String getPointString(int i) { return (i=0) ? ""+(int)xt[i]+"," +(int)yt[i]+", " +(int)zt[i] : "Invalid Range"; } /*===============================================================*/ /** * * getRadius() -- return the radius * * Precondition: the radius has been set * * Postcondition: the radius is returned * * @return radius -- the radius of all points. * */ public int getRadius() { return radius; } /*===============================================================*/ /** * * getX() -- return the array of x coordinates * * Precondition: the x array has been instantiated * * Postcondition: the transformed x array is returned * * @param xt -- the transformed array of x coordinates * */ public double[] getX() { return xt; } /*===============================================================*/ /** * * getY() -- return the array of y coordinates * * Precondition: the y array has been instanitated * * Postcondition: the transformed y array is returned * * @return yt -- the transformed array of y coordinates * */ public double[] getY() { return yt; } /*===============================================================*/ /** * * getZ() -- return the array of z coordinates * * Precondition: the z array has been instanitated * * Postcondition: the transformed z array is returned * * @return zt -- the transformed array of z coordinates * */ public double[] getZ() { return zt; } /*===============================================================*/ /** * getPointCount() -- return the number of points in this instance * * Precondition: points have been added * * Postcondition: the point count is returned * * @return pts -- the number of points in this instance */ public int getPointCount() { return pts; } /*===============================================================*/ /** * getLineCount() -- return the number of lines in this instance * * Precondition: some points are connected as lines * * Postcondition: the number of lines is returned * * @return lns -- the number of lines */ public int getLineCount() { return lns; } /*===============================================================*/ /** * getLines() -- return the array of lines * * Precondition: array of lines has been instaniated * * Postcondition: the array holding the lines is returned * * @return lines int[][] -- the array of lines * */ public int[][] getLines() { return lines; } /*===============================================================*/ }