<< Chapter < Page Chapter >> Page >

Complete program listings

Complete listings of the programs discussed in this module are shown in Listing 30 through Listing 34 below.

Listing 30 . Source code for the game-math library named GM01.

/*GM01.java Copyright 2008, R.G.BaldwinRevised 02/24/08 This is a major upgrade to the game-math library. Thisversion upgrades the version named GM2D04 to a new version named simply GM01.The primary purpose of the upgrade was to add 3D capability for all of the 2D features provided by theprevious version. Because both 2D and 3D capabilities are included, it is no longer necessary to differentiatebetween the two in the name of the class. Therefore, this version is named GM01.Adding 3D capability entailed major complexity in one particular area: drawing the objects. It is difficult todraw a 3D object on a 2D screen. This requires a projection process to project each point in the 3D objectonto the correct location on a 2D plane. There are a variety of ways to do this. This 3D library uses anapproach often referred to as an oblique parallel projection. See the following URL for technicalinformation on the projection process: http://local.wasp.uwa.edu.au/~pbourke/geometry/classification/ In addition to adding 3D capability, this version alsoeliminates the confusion surrounding the fact that the default direction of the positive y-axis is going downthe screen instead of up the screen as viewers have become accustomed to. When you use this library, you can programunder the assumption that the positive direction of the y-axis is up the screen, provided you funnel all of yourdrawing tasks through the library and don't draw directly on the screen.The name GMnn is an abbreviation for GameMathnn. See the file named GM2D01.java for a general descriptionof the game-math library. The library has been updated several times. This file is an update of GM2D04.In addition to the updates mentioned above, this update cleaned up some lingering areas of code inefficiency,using the simplest available method to draw on an off-screen image. In addition, the following new methodswere added: The following methods are new static methods of the classnamed GM01. The first method in the list deals with the problem of displaying a 3D image on a 3D screen.The last five methods in the list wrap the standard graphics methods for the purpose of eliminating the issueof the direction of the positive Y-axis. GM01.convert3Dto2DGM01.translate GM01.drawLineGM01.fillOval GM01.drawOvalGM01.fillRect The following methods are new instance methods of theindicated static top-level classes belonging to the class named GM01.GM01.Vector2D.scale GM01.Vector2D.negateGM01.Point2D.clone GM01.Vector2D.normalizeGM01.Point2D.rotate GM01.Point2D.scaleGM01.Vector3D.scale GM01.Vector3D.negateGM01.Point3D.clone GM01.Vector3D.normalizeGM01.Point3D.rotate GM01.Point3D.scaleTested using JDK 1.6 under WinXP. *********************************************************/import java.awt.geom.*; import java.awt.*;public class GM01{ //----------------------------------------------------////This method converts a ColMatrix3D object to a// ColMatrix2D object. The purpose is to accept // x, y, and z coordinate values and transform those// values into a pair of coordinate values suitable for // display in two dimensions.//See http://local.wasp.uwa.edu.au/~pbourke/geometry/ // classification/ for technical background on the// transform from 3D to 2D. //The transform equations are:// x2d = x3d + z3d * cos(theta)/tan(alpha) // y2d = y3d + z3d * sin(theta)/tan(alpha);//Let theta = 30 degrees and alpha = 45 degrees //Then:cos(theta) = 0.866// sin(theta) = 0.5 // tan(alpha) = 1;//Note that the signs in the above equations depend // on the assumed directions of the angles as well as// the assumed positive directions of the axes. The // signs used in this method assume the following:// Positive x is to the right. // Positive y is up the screen.// Positive z is protruding out the front of the // screen.// The viewing position is above the x axis and to the // right of the z-y plane.public static GM01.ColMatrix2D convert3Dto2D( GM01.ColMatrix3D data){return new GM01.ColMatrix2D( data.getData(0) - 0.866*data.getData(2),data.getData(1) - 0.50*data.getData(2)); }//end convert3Dto2D//----------------------------------------------------////This method wraps around the translate method of the // Graphics2D class. The purpose is to cause the// positive direction for the y-axis to be up the screen // instead of down the screen. When you use this method,// you should program as though the positive direction // for the y-axis is up.public static void translate(Graphics2D g2D, double xOffset,double yOffset){ //Flip the sign on the y-coordinate to change the// direction of the positive y-axis to go up the // screen.g2D.translate(xOffset,-yOffset); }//end translate//----------------------------------------------------////This method wraps around the drawLine method of the // Graphics class. The purpose is to cause the positive// direction for the y-axis to be up the screen instead // of down the screen. When you use this method, you// should program as though the positive direction for // the y-axis is up.public static void drawLine(Graphics2D g2D, double x1,double y1, double x2,double y2){ //Flip the sign on the y-coordinate value.g2D.drawLine((int)x1,-(int)y1,(int)x2,-(int)y2); }//end drawLine//----------------------------------------------------////This method wraps around the fillOval method of the // Graphics class. The purpose is to cause the positive// direction for the y-axis to be up the screen instead // of down the screen. When you use this method, you// should program as though the positive direction for // the y-axis is up.public static void fillOval(Graphics2D g2D, double x,double y, double width,double height){ //Flip the sign on the y-coordinate value.g2D.fillOval((int)x,-(int)y,(int)width,(int)height); }//end fillOval//----------------------------------------------------// //This method wraps around the drawOval method of the// Graphics class. The purpose is to cause the positive // direction for the y-axis to be up the screen instead// of down the screen. When you use this method, you // should program as though the positive direction for// the y-axis is up. public static void drawOval(Graphics2D g2D,double x, double y,double width, double height){//Flip the sign on the y-coordinate value. g2D.drawOval((int)x,-(int)y,(int)width,(int)height);}//end drawOval //----------------------------------------------------////This method wraps around the fillRect method of the// Graphics class. The purpose is to cause the positive // direction for the y-axis to be up the screen instead// of down the screen. When you use this method, you // should program as though the positive direction for// the y-axis is up. public static void fillRect(Graphics2D g2D,double x, double y,double width, double height){//Flip the sign on the y-coordinate value. g2D.fillRect((int)x,-(int)y,(int)width,(int)height);}//end fillRect//----------------------------------------------------////An object of this class represents a 2D column matrix.// An object of this class is the fundamental building // block for several of the other classes in the// library. public static class ColMatrix2D{double[] data = new double[2];public ColMatrix2D(double data0,double data1){ data[0]= data0; data[1]= data1; }//end constructor//--------------------------------------------------////Overridden toString method. public String toString(){return data[0] + "," + data[1]; }//end overridden toString method//--------------------------------------------------//public double getData(int index){ if((index<0) || (index>1)){ throw new IndexOutOfBoundsException();}else{ return data[index]; }//end else}//end getData method //--------------------------------------------------//public void setData(int index,double data){if((index<0) || (index>1)){ throw new IndexOutOfBoundsException();}else{ this.data[index]= data; }//end else}//end setData method //--------------------------------------------------////This method overrides the equals method inherited// from the class named Object. It compares the values // stored in two matrices and returns true if the// values are equal or almost equal and returns false // otherwise.public boolean equals(Object obj){ if(obj instanceof GM01.ColMatrix2D&&Math.abs(((GM01.ColMatrix2D)obj).getData(0) - getData(0))<= 0.00001&&Math.abs(((GM01.ColMatrix2D)obj).getData(1) - getData(1))<= 0.00001){ return true;}else{ return false;}//end else}//end overridden equals method //--------------------------------------------------////Adds one ColMatrix2D object to another ColMatrix2D // object, returning a ColMatrix2D object.public GM01.ColMatrix2D add(GM01.ColMatrix2D matrix){ return new GM01.ColMatrix2D(getData(0)+matrix.getData(0), getData(1)+matrix.getData(1));}//end add //--------------------------------------------------////Subtracts one ColMatrix2D object from another// ColMatrix2D object, returning a ColMatrix2D object. // The object that is received as an incoming// parameter is subtracted from the object on which // the method is called.public GM01.ColMatrix2D subtract( GM01.ColMatrix2D matrix){return new GM01.ColMatrix2D( getData(0)-matrix.getData(0),getData(1)-matrix.getData(1)); }//end subtract//--------------------------------------------------// }//end class ColMatrix2D//====================================================// //An object of this class represents a 3D column matrix.// An object of this class is the fundamental building // block for several of the other classes in the// library. public static class ColMatrix3D{double[] data = new double[3];public ColMatrix3D( double data0,double data1,double data2){data[0] = data0;data[1] = data1;data[2] = data2;}//end constructor //--------------------------------------------------//public String toString(){return data[0] + "," + data[1]+ "," + data[2];}//end overridden toString method //--------------------------------------------------//public double getData(int index){if((index<0) || (index>2)){ throw new IndexOutOfBoundsException();}else{ return data[index]; }//end else}//end getData method //--------------------------------------------------//public void setData(int index,double data){if((index<0) || (index>2)){ throw new IndexOutOfBoundsException();}else{ this.data[index]= data; }//end else}//end setData method //--------------------------------------------------////This method overrides the equals method inherited// from the class named Object. It compares the values // stored in two matrices and returns true if the// values are equal or almost equal and returns false // otherwise.public boolean equals(Object obj){ if(obj instanceof GM01.ColMatrix3D&&Math.abs(((GM01.ColMatrix3D)obj).getData(0) - getData(0))<= 0.00001&&Math.abs(((GM01.ColMatrix3D)obj).getData(1) - getData(1))<= 0.00001&&Math.abs(((GM01.ColMatrix3D)obj).getData(2) - getData(2))<= 0.00001){ return true;}else{ return false;}//end else}//end overridden equals method //--------------------------------------------------////Adds one ColMatrix3D object to another ColMatrix3D // object, returning a ColMatrix3D object.public GM01.ColMatrix3D add(GM01.ColMatrix3D matrix){ return new GM01.ColMatrix3D(getData(0)+matrix.getData(0), getData(1)+matrix.getData(1),getData(2)+matrix.getData(2)); }//end add//--------------------------------------------------////Subtracts one ColMatrix3D object from another // ColMatrix3D object, returning a ColMatrix3D object.// The object that is received as an incoming // parameter is subtracted from the object on which// the method is called. public GM01.ColMatrix3D subtract(GM01.ColMatrix3D matrix){ return new GM01.ColMatrix3D(getData(0)-matrix.getData(0), getData(1)-matrix.getData(1),getData(2)-matrix.getData(2)); }//end subtract//--------------------------------------------------// }//end class ColMatrix3D//====================================================// //====================================================//public static class Point2D{ GM01.ColMatrix2D point;public Point2D(GM01.ColMatrix2D point){//constructor//Create and save a clone of the ColMatrix2D object // used to define the point to prevent the point// from being corrupted by a later change in the // values stored in the original ColMatrix2D object// through use of its set method. this.point = new ColMatrix2D(point.getData(0),point.getData(1)); }//end constructor//--------------------------------------------------// public String toString(){return point.getData(0) + "," + point.getData(1); }//end toString//--------------------------------------------------//public double getData(int index){ if((index<0) || (index>1)){ throw new IndexOutOfBoundsException();}else{ return point.getData(index);}//end else }//end getData//--------------------------------------------------//public void setData(int index,double data){ if((index<0) || (index>1)){ throw new IndexOutOfBoundsException();}else{ point.setData(index,data);}//end else }//end setData//--------------------------------------------------// //This method draws a small circle around the location// of the point on the specified graphics context. public void draw(Graphics2D g2D){drawOval(g2D,getData(0)-3, getData(1)+3,6,6);}//end draw //--------------------------------------------------////Returns a reference to the ColMatrix2D object that// defines this Point2D object. public GM01.ColMatrix2D getColMatrix(){ return point;}//end getColMatrix //--------------------------------------------------////This method overrides the equals method inherited // from the class named Object. It compares the values// stored in the ColMatrix2D objects that define two // Point2D objects and returns true if they are equal// and false otherwise. public boolean equals(Object obj){if(point.equals(((GM01.Point2D)obj). getColMatrix())){return true; }else{return false; }//end else}//end overridden equals method//--------------------------------------------------// //Gets a displacement vector from one Point2D object// to a second Point2D object. The vector points from // the object on which the method is called to the// object passed as a parameter to the method. Kjell // describes this as the distance you would have to// walk along the x and then the y axes to get from // the first point to the second point.public GM01.Vector2D getDisplacementVector( GM01.Point2D point){return new GM01.Vector2D(new GM01.ColMatrix2D( point.getData(0)-getData(0),point.getData(1)-getData(1))); }//end getDisplacementVector//--------------------------------------------------////Adds a Vector2D to a Point2D producing a // new Point2D.public GM01.Point2D addVectorToPoint( GM01.Vector2D vec){return new GM01.Point2D(new GM01.ColMatrix2D( getData(0) + vec.getData(0),getData(1) + vec.getData(1))); }//end addVectorToPoint//--------------------------------------------------////Returns a new Point2D object that is a clone of // the object on which the method is called.public Point2D clone(){ return new Point2D(new ColMatrix2D(getData(0),getData(1))); }//end clone//--------------------------------------------------////The purpose of this method is to rotate a point // around a specified anchor point in the x-y plane.//The rotation angle is passed in as a double value // in degrees with the positive angle of rotation// being counter-clockwise. //This method does not modify the contents of the// Point2D object on which the method is called. // Rather, it uses the contents of that object to// instantiate, rotate, and return a new Point2D // object.//For simplicity, this method translates the // anchorPoint to the origin, rotates around the// origin, and then translates back to the // anchorPoint./* See http://www.ia.hiof.no/~borres/cgraph/math/threed/p-threed.html for a definition of the equations required to do the rotation.x2 = x1*cos - y1*sin y2 = x1*sin + y1*cos*/ public GM01.Point2D rotate(GM01.Point2D anchorPoint,double angle){ GM01.Point2D newPoint = this.clone();double tempX ;double tempY;//Translate anchorPoint to the origin GM01.Vector2D tempVec =new GM01.Vector2D(anchorPoint.getColMatrix()); newPoint =newPoint.addVectorToPoint(tempVec.negate());//Rotate around the origin. tempX = newPoint.getData(0);tempY = newPoint.getData(1); newPoint.setData(//new x coordinate0, tempX*Math.cos(angle*Math.PI/180) -tempY*Math.sin(angle*Math.PI/180)); newPoint.setData(//new y coordinate1, tempX*Math.sin(angle*Math.PI/180) +tempY*Math.cos(angle*Math.PI/180)); //Translate back to anchorPointnewPoint = newPoint.addVectorToPoint(tempVec);return newPoint; }//end rotate//--------------------------------------------------////Multiplies this point by a scaling matrix received // as an incoming parameter and returns the scaled// point. public GM01.Point2D scale(GM01.ColMatrix2D scale){return new GM01.Point2D(new ColMatrix2D( getData(0) * scale.getData(0),getData(1) * scale.getData(1))); }//end scale//--------------------------------------------------// }//end class Point2D//====================================================// public static class Point3D{GM01.ColMatrix3D point;public Point3D(GM01.ColMatrix3D point){//constructor //Create and save a clone of the ColMatrix3D object// used to define the point to prevent the point // from being corrupted by a later change in the// values stored in the original ColMatrix3D object // through use of its set method.this.point = new ColMatrix3D(point.getData(0),point.getData(1), point.getData(2));}//end constructor //--------------------------------------------------//public String toString(){ return point.getData(0) + "," + point.getData(1)+ "," + point.getData(2); }//end toString//--------------------------------------------------//public double getData(int index){ if((index<0) || (index>2)){ throw new IndexOutOfBoundsException();}else{ return point.getData(index);}//end else }//end getData//--------------------------------------------------//public void setData(int index,double data){ if((index<0) || (index>2)){ throw new IndexOutOfBoundsException();}else{ point.setData(index,data);}//end else }//end setData//--------------------------------------------------// //This method draws a small circle around the location// of the point on the specified graphics context. public void draw(Graphics2D g2D){//Get 2D projection coordinate values.ColMatrix2D temp = convert3Dto2D(point); drawOval(g2D,temp.getData(0)-3,temp.getData(1)+3, 6,6); }//end draw//--------------------------------------------------////Returns a reference to the ColMatrix3D object that // defines this Point3D object.public GM01.ColMatrix3D getColMatrix(){ return point;}//end getColMatrix //--------------------------------------------------////This method overrides the equals method inherited // from the class named Object. It compares the values// stored in the ColMatrix3D objects that define two // Point3D objects and returns true if they are equal// and false otherwise. public boolean equals(Object obj){if(point.equals(((GM01.Point3D)obj). getColMatrix())){return true; }else{return false; }//end else}//end overridden equals method//--------------------------------------------------// //Gets a displacement vector from one Point3D object// to a second Point3D object. The vector points from // the object on which the method is called to the// object passed as a parameter to the method. Kjell // describes this as the distance you would have to// walk along the x and then the y axes to get from // the first point to the second point.public GM01.Vector3D getDisplacementVector( GM01.Point3D point){return new GM01.Vector3D(new GM01.ColMatrix3D( point.getData(0)-getData(0),point.getData(1)-getData(1), point.getData(2)-getData(2)));}//end getDisplacementVector //--------------------------------------------------////Adds a Vector3D to a Point3D producing a// new Point3D. public GM01.Point3D addVectorToPoint(GM01.Vector3D vec){ return new GM01.Point3D(new GM01.ColMatrix3D(getData(0) + vec.getData(0), getData(1) + vec.getData(1),getData(2) + vec.getData(2))); }//end addVectorToPoint//--------------------------------------------------////Returns a new Point3D object that is a clone of // the object on which the method is called.public Point3D clone(){ return new Point3D(new ColMatrix3D(getData(0),getData(1), getData(2)));}//end clone //--------------------------------------------------////The purpose of this method is to rotate a point// around a specified anchor point in the following // order:// Rotate around z - rotation in x-y plane. // Rotate around x - rotation in y-z plane.// Rotate around y - rotation in x-z plane. //The rotation angles are passed in as double values// in degrees (based on the right-hand rule) in the // order given above, packaged in an object of the// class GM01.ColMatrix3D. (Note that in this case, // the ColMatrix3D object is simply a convenient// container and it has no significance from a matrix // viewpoint.)//The right-hand rule states that if you point the // thumb of your right hand in the positive direction// of an axis, the direction of positive rotation // around that axis is given by the direction that// your fingers will be pointing. //This method does not modify the contents of the// Point3D object on which the method is called. // Rather, it uses the contents of that object to// instantiate, rotate, and return a new Point3D // object.//For simplicity, this method translates the // anchorPoint to the origin, rotates around the// origin, and then translates back to the // anchorPoint./* See http://www.ia.hiof.no/~borres/cgraph/math/threed/p-threed.html for a definition of the equations required to do the rotation.z-axis x2 = x1*cos - y1*siny2 = x1*sin + y1*cosx-axis y2 = y1*cos(v) - z1*sin(v)z2 = y1*sin(v) + z1* cos(v)y-axis x2 = x1*cos(v) + z1*sin(v)z2 = -x1*sin(v) + z1*cos(v) */public GM01.Point3D rotate(GM01.Point3D anchorPoint, GM01.ColMatrix3D angles){GM01.Point3D newPoint = this.clone();double tempX ; double tempY;double tempZ;//Translate anchorPoint to the origin GM01.Vector3D tempVec =new GM01.Vector3D(anchorPoint.getColMatrix()); newPoint =newPoint.addVectorToPoint(tempVec.negate()); double zAngle = angles.getData(0);double xAngle = angles.getData(1); double yAngle = angles.getData(2);//Rotate around z-axistempX = newPoint.getData(0); tempY = newPoint.getData(1);newPoint.setData(//new x coordinate 0,tempX*Math.cos(zAngle*Math.PI/180) - tempY*Math.sin(zAngle*Math.PI/180));newPoint.setData(//new y coordinate 1,tempX*Math.sin(zAngle*Math.PI/180) + tempY*Math.cos(zAngle*Math.PI/180));//Rotate around x-axistempY = newPoint.getData(1); tempZ = newPoint.getData(2);newPoint.setData(//new y coordinate 1,tempY*Math.cos(xAngle*Math.PI/180) - tempZ*Math.sin(xAngle*Math.PI/180));newPoint.setData(//new z coordinate 2,tempY*Math.sin(xAngle*Math.PI/180) + tempZ*Math.cos(xAngle*Math.PI/180));//Rotate around y-axistempX = newPoint.getData(0); tempZ = newPoint.getData(2);newPoint.setData(//new x coordinate 0,tempX*Math.cos(yAngle*Math.PI/180) + tempZ*Math.sin(yAngle*Math.PI/180));newPoint.setData(//new z coordinate 2,-tempX*Math.sin(yAngle*Math.PI/180) + tempZ*Math.cos(yAngle*Math.PI/180));//Translate back to anchorPointnewPoint = newPoint.addVectorToPoint(tempVec);return newPoint; }//end rotate//--------------------------------------------------////Multiplies this point by a scaling matrix received // as an incoming parameter and returns the scaled// point. public GM01.Point3D scale(GM01.ColMatrix3D scale){return new GM01.Point3D(new ColMatrix3D( getData(0) * scale.getData(0),getData(1) * scale.getData(1), getData(2) * scale.getData(2)));}//end scale //--------------------------------------------------//}//end class Point3D //====================================================////====================================================//public static class Vector2D{ GM01.ColMatrix2D vector;public Vector2D(GM01.ColMatrix2D vector){//constructor//Create and save a clone of the ColMatrix2D object // used to define the vector to prevent the vector// from being corrupted by a later change in the // values stored in the original ColVector2D object.this.vector = new ColMatrix2D( vector.getData(0),vector.getData(1));}//end constructor //--------------------------------------------------//public String toString(){ return vector.getData(0) + "," + vector.getData(1);}//end toString //--------------------------------------------------//public double getData(int index){if((index<0) || (index>1)){ throw new IndexOutOfBoundsException();}else{ return vector.getData(index);}//end else }//end getData//--------------------------------------------------//public void setData(int index,double data){ if((index<0) || (index>1)){ throw new IndexOutOfBoundsException();}else{ vector.setData(index,data);}//end else }//end setData//--------------------------------------------------////This method draws a vector on the specified graphics // context, with the tail of the vector located at a// specified point, and with a small filled circle at // the head.public void draw(Graphics2D g2D,GM01.Point2D tail){ drawLine(g2D,tail.getData(0), tail.getData(1),tail.getData(0)+vector.getData(0), tail.getData(1)+vector.getData(1));fillOval(g2D, tail.getData(0)+vector.getData(0)-3,tail.getData(1)+vector.getData(1)+3, 6,6); }//end draw//--------------------------------------------------////Returns a reference to the ColMatrix2D object that // defines this Vector2D object.public GM01.ColMatrix2D getColMatrix(){ return vector;}//end getColMatrix //--------------------------------------------------////This method overrides the equals method inherited // from the class named Object. It compares the values// stored in the ColMatrix2D objects that define two // Vector2D objects and returns true if they are equal// and false otherwise. public boolean equals(Object obj){if(vector.equals(( (GM01.Vector2D)obj).getColMatrix())){return true; }else{return false; }//end else}//end overridden equals method//--------------------------------------------------////Adds this vector to a vector received as an incoming // parameter and returns the sum as a vector.public GM01.Vector2D add(GM01.Vector2D vec){ return new GM01.Vector2D(new ColMatrix2D( vec.getData(0)+vector.getData(0),vec.getData(1)+vector.getData(1))); }//end add//--------------------------------------------------////Returns the length of a Vector2D object. public double getLength(){return Math.sqrt( getData(0)*getData(0) + getData(1)*getData(1));}//end getLength //--------------------------------------------------////Multiplies this vector by a scale factor received as// an incoming parameter and returns the scaled // vector.public GM01.Vector2D scale(Double factor){ return new GM01.Vector2D(new ColMatrix2D(getData(0) * factor, getData(1) * factor));}//end scale //--------------------------------------------------////Changes the sign on each of the vector components// and returns the negated vector. public GM01.Vector2D negate(){return new GM01.Vector2D(new ColMatrix2D( -getData(0),-getData(1))); }//end negate//--------------------------------------------------////Returns a new vector that points in the same // direction but has a length of one unit.public GM01.Vector2D normalize(){ double length = getLength();return new GM01.Vector2D(new ColMatrix2D( getData(0)/length,getData(1)/length)); }//end normalize//--------------------------------------------------// }//end class Vector2D//====================================================// public static class Vector3D{GM01.ColMatrix3D vector;public Vector3D(GM01.ColMatrix3D vector){//constructor //Create and save a clone of the ColMatrix3D object// used to define the vector to prevent the vector // from being corrupted by a later change in the// values stored in the original ColMatris3D object. this.vector = new ColMatrix3D(vector.getData(0),vector.getData(1), vector.getData(2));}//end constructor //--------------------------------------------------//public String toString(){ return vector.getData(0) + "," + vector.getData(1)+ "," + vector.getData(2); }//end toString//--------------------------------------------------//public double getData(int index){ if((index<0) || (index>2)){ throw new IndexOutOfBoundsException();}else{ return vector.getData(index);}//end else }//end getData//--------------------------------------------------//public void setData(int index,double data){ if((index<0) || (index>2)){ throw new IndexOutOfBoundsException();}else{ vector.setData(index,data);}//end else }//end setData//--------------------------------------------------////This method draws a vector on the specified graphics // context, with the tail of the vector located at a// specified point, and with a small circle at the // head.public void draw(Graphics2D g2D,GM01.Point3D tail){//Get a 2D projection of the tail GM01.ColMatrix2D tail2D = convert3Dto2D(tail.point);//Get the 3D location of the headGM01.ColMatrix3D head = tail.point.add(this.getColMatrix());//Get a 2D projection of the headGM01.ColMatrix2D head2D = convert3Dto2D(head); drawLine(g2D,tail2D.getData(0),tail2D.getData(1), head2D.getData(0),head2D.getData(1)); //Draw a small filled circle to identify the head.fillOval(g2D,head2D.getData(0)-3, head2D.getData(1)+3,6, 6);}//end draw //--------------------------------------------------////Returns a reference to the ColMatrix3D object that// defines this Vector3D object. public GM01.ColMatrix3D getColMatrix(){return vector; }//end getColMatrix//--------------------------------------------------// //This method overrides the equals method inherited// from the class named Object. It compares the values // stored in the ColMatrix3D objects that define two// Vector3D objects and returns true if they are equal // and false otherwise.public boolean equals(Object obj){ if(vector.equals(((GM01.Vector3D)obj).getColMatrix())){ return true;}else{ return false;}//end else}//end overridden equals method //--------------------------------------------------////Adds this vector to a vector received as an incoming// parameter and returns the sum as a vector. public GM01.Vector3D add(GM01.Vector3D vec){return new GM01.Vector3D(new ColMatrix3D( vec.getData(0)+vector.getData(0),vec.getData(1)+vector.getData(1), vec.getData(2)+vector.getData(2)));}//end add //--------------------------------------------------////Returns the length of a Vector3D object.public double getLength(){ return Math.sqrt(getData(0)*getData(0) +getData(1)*getData(1) + getData(2)*getData(2));}//end getLength //--------------------------------------------------////Multiplies this vector by a scale factor received as// an incoming parameter and returns the scaled // vector.public GM01.Vector3D scale(Double factor){ return new GM01.Vector3D(new ColMatrix3D(getData(0) * factor, getData(1) * factor,getData(2) * factor)); }//end scale//--------------------------------------------------////Changes the sign on each of the vector components // and returns the negated vector.public GM01.Vector3D negate(){ return new GM01.Vector3D(new ColMatrix3D(-getData(0), -getData(1),-getData(2))); }//end negate//--------------------------------------------------////Returns a new vector that points in the same // direction but has a length of one unit.public GM01.Vector3D normalize(){ double length = getLength();return new GM01.Vector3D(new ColMatrix3D( getData(0)/length,getData(1)/length, getData(2)/length));}//end normalize //--------------------------------------------------//}//end class Vector3D //====================================================////====================================================////A line is defined by two points. One is called the // tail and the other is called the head. Note that this// class has the same name as one of the classes in // the Graphics2D class. Therefore, if the class from// the Graphics2D class is used in some future upgrade // to this program, it will have to be fully qualified.public static class Line2D{ GM01.Point2D[]line = new GM01.Point2D[2];public Line2D(GM01.Point2D tail,GM01.Point2D head){//Create and save clones of the points used to // define the line to prevent the line from being// corrupted by a later change in the coordinate // values of the points.this.line[0] = new Point2D(new GM01.ColMatrix2D(tail.getData(0),tail.getData(1))); this.line[1]= new Point2D(new GM01.ColMatrix2D( head.getData(0),head.getData(1)));}//end constructor//--------------------------------------------------// public String toString(){return "Tail = " + line[0].getData(0) + ","+ line[0].getData(1) + "\nHead = "+ line[1].getData(0) + ","+ line[1].getData(1);}//end toString //--------------------------------------------------//public GM01.Point2D getTail(){ return line[0]; }//end getTail//--------------------------------------------------//public GM01.Point2D getHead(){ return line[1]; }//end getHead//--------------------------------------------------//public void setTail(GM01.Point2D newPoint){ //Create and save a clone of the new point to// prevent the line from being corrupted by a // later change in the coordinate values of the// point. this.line[0]= new Point2D(new GM01.ColMatrix2D( newPoint.getData(0),newPoint.getData(1)));}//end setTail //--------------------------------------------------//public void setHead(GM01.Point2D newPoint){//Create and save a clone of the new point to // prevent the line from being corrupted by a// later change in the coordinate values of the // point.this.line[1] = new Point2D(new GM01.ColMatrix2D(newPoint.getData(0),newPoint.getData(1))); }//end setHead//--------------------------------------------------//public void draw(Graphics2D g2D){ drawLine(g2D,getTail().getData(0),getTail().getData(1), getHead().getData(0),getHead().getData(1)); }//end draw//--------------------------------------------------// }//end class Line2D//====================================================// //A line is defined by two points. One is called the// tail and the other is called the head. public static class Line3D{GM01.Point3D[] line = new GM01.Point3D[2];public Line3D(GM01.Point3D tail,GM01.Point3D head){ //Create and save clones of the points used to// define the line to prevent the line from being // corrupted by a later change in the coordinate// values of the points. this.line[0]= new Point3D(new GM01.ColMatrix3D( tail.getData(0),tail.getData(1), tail.getData(2)));this.line[1] = new Point3D(new GM01.ColMatrix3D(head.getData(0), head.getData(1),head.getData(2))); }//end constructor//--------------------------------------------------// public String toString(){return "Tail = " + line[0].getData(0) + ","+ line[0].getData(1) + ","+ line[0].getData(2)+ "\nHead = " + line[1].getData(0) + "," + line[1].getData(1) + "," + line[1].getData(2); }//end toString//--------------------------------------------------// public GM01.Point3D getTail(){return line[0];}//end getTail //--------------------------------------------------//public GM01.Point3D getHead(){return line[1];}//end getHead //--------------------------------------------------//public void setTail(GM01.Point3D newPoint){ //Create and save a clone of the new point to// prevent the line from being corrupted by a // later change in the coordinate values of the// point. this.line[0]= new Point3D(new GM01.ColMatrix3D( newPoint.getData(0),newPoint.getData(1), newPoint.getData(2)));}//end setTail //--------------------------------------------------//public void setHead(GM01.Point3D newPoint){//Create and save a clone of the new point to // prevent the line from being corrupted by a// later change in the coordinate values of the // point.this.line[1] = new Point3D(new GM01.ColMatrix3D(newPoint.getData(0), newPoint.getData(1),newPoint.getData(2))); }//end setHead//--------------------------------------------------// public void draw(Graphics2D g2D){//Get 2D projection coordinates.GM01.ColMatrix2D tail = convert3Dto2D(getTail().point);GM01.ColMatrix2D head = convert3Dto2D(getHead().point);drawLine(g2D,tail.getData(0), tail.getData(1),head.getData(0), head.getData(1));}//end draw //--------------------------------------------------//}//end class Line3D //====================================================//}//end class GM01 //======================================================//

Questions & Answers

the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
what is cell divisoin?
Aron Reply
Diversity of living thing
ISCONT
what is cell division
Aron Reply
Cell division is the process by which a single cell divides into two or more daughter cells. It is a fundamental process in all living organisms and is essential for growth, development, and reproduction. Cell division can occur through either mitosis or meiosis.
AI-Robot
What is life?
Allison Reply
life is defined as any system capable of performing functions such as eating, metabolizing,excreting,breathing,moving,Growing,reproducing,and responding to external stimuli.
Mohamed
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask