<< Chapter < Page | Chapter >> Page > |
Complete listings of the programs discussed in this module are shown in Listing 9 through Listing 13 below.
Listing 9 . Source code for the game-math library named GM02.
/*GM02.java
Copyright 2008, R.G.BaldwinRevised 02/08/08
This is an update to the game-math library named GM01.The main purpose of this update was to add vector dot
product and related capabilities to the library.Please see the comments at the beginning of the library
class named GM01 for a general description of the library.The following methods are new instance methods of the
indicated static top-level classes belonging to the classnamed GM02.
GM02.ColMatrix2D.dot - compute dot product of twoColMatrix2D objects.
GM02.Vector2D.dot - compute dot product of twoVector2D objects.
GM02.Vector2D.angle - compute angle between two Vector2Dobjects.
GM02.ColMatrix3D.dot - compute dot product of twoColMatrix3D objects
GM02.Vector3D.dot - compute dot product of twoVector3D objects.
GM02.Vector3D.angle - compute angle between two Vector3Dobjects.
Tested using JDK 1.6 under WinXP.*********************************************************/
import java.awt.geom.*;import java.awt.*;
public class GM02{//----------------------------------------------------////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 GM02.ColMatrix2D convert3Dto2D(GM02.ColMatrix3D data){
return new GM02.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 GM02.ColMatrix2D&&Math.abs(((GM02.ColMatrix2D)obj).getData(0) -
getData(0))<= 0.00001&&Math.abs(((GM02.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 GM02.ColMatrix2D add(GM02.ColMatrix2D matrix){
return new GM02.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 GM02.ColMatrix2D subtract(
GM02.ColMatrix2D matrix){return new GM02.ColMatrix2D(
getData(0)-matrix.getData(0),getData(1)-matrix.getData(1));
}//end subtract//--------------------------------------------------////Computes the dot product of two ColMatrix2D
// objects and returns the result as type double.public double dot(GM02.ColMatrix2D matrix){
return getData(0) * matrix.getData(0)+ getData(1) * matrix.getData(1);
}//end dot//--------------------------------------------------//
}//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 GM02.ColMatrix3D&&Math.abs(((GM02.ColMatrix3D)obj).getData(0) -
getData(0))<= 0.00001&&Math.abs(((GM02.ColMatrix3D)obj).getData(1) -
getData(1))<= 0.00001&&Math.abs(((GM02.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 GM02.ColMatrix3D add(GM02.ColMatrix3D matrix){
return new GM02.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 GM02.ColMatrix3D subtract(GM02.ColMatrix3D matrix){
return new GM02.ColMatrix3D(getData(0)-matrix.getData(0),
getData(1)-matrix.getData(1),getData(2)-matrix.getData(2));
}//end subtract//--------------------------------------------------////Computes the dot product of two ColMatrix3D
// objects and returns the result as type double.public double dot(GM02.ColMatrix3D matrix){
return getData(0) * matrix.getData(0)+ getData(1) * matrix.getData(1)
+ getData(2) * matrix.getData(2);}//end dot
//--------------------------------------------------//}//end class ColMatrix3D
//====================================================////====================================================//public static class Point2D{
GM02.ColMatrix2D point;public Point2D(GM02.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 GM02.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(((GM02.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 GM02.Vector2D getDisplacementVector(GM02.Point2D point){
return new GM02.Vector2D(new GM02.ColMatrix2D(point.getData(0)-getData(0),
point.getData(1)-getData(1)));}//end getDisplacementVector
//--------------------------------------------------////Adds a Vector2D to a Point2D producing a// new Point2D.
public GM02.Point2D addVectorToPoint(GM02.Vector2D vec){
return new GM02.Point2D(new GM02.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 equationsrequired to do the rotation.
x2 = x1*cos - y1*siny2 = x1*sin + y1*cos
*/public GM02.Point2D rotate(GM02.Point2D anchorPoint,
double angle){GM02.Point2D newPoint = this.clone();double tempX ;
double tempY;//Translate anchorPoint to the originGM02.Vector2D tempVec =
new GM02.Vector2D(anchorPoint.getColMatrix());newPoint =
newPoint.addVectorToPoint(tempVec.negate());//Rotate around the origin.tempX = newPoint.getData(0);
tempY = newPoint.getData(1);newPoint.setData(//new x coordinate
0,tempX*Math.cos(angle*Math.PI/180) -
tempY*Math.sin(angle*Math.PI/180));newPoint.setData(//new y coordinate
1,tempX*Math.sin(angle*Math.PI/180) +
tempY*Math.cos(angle*Math.PI/180));//Translate back to anchorPoint
newPoint = 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 GM02.Point2D scale(GM02.ColMatrix2D scale){
return new GM02.Point2D(new ColMatrix2D(getData(0) * scale.getData(0),
getData(1) * scale.getData(1)));}//end scale
//--------------------------------------------------//}//end class Point2D
//====================================================//public static class Point3D{
GM02.ColMatrix3D point;public Point3D(GM02.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 GM02.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(((GM02.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 GM02.Vector3D getDisplacementVector(GM02.Point3D point){
return new GM02.Vector3D(new GM02.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 GM02.Point3D addVectorToPoint(
GM02.Vector3D vec){return new GM02.Point3D(new GM02.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 GM02.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 equationsrequired to do the rotation.
z-axisx2 = x1*cos - y1*sin
y2 = x1*sin + y1*cosx-axisy2 = y1*cos(v) - z1*sin(v)
z2 = y1*sin(v) + z1* cos(v)y-axisx2 = x1*cos(v) + z1*sin(v)
z2 = -x1*sin(v) + z1*cos(v)*/
public GM02.Point3D rotate(GM02.Point3D anchorPoint,GM02.ColMatrix3D angles){
GM02.Point3D newPoint = this.clone();double tempX ;double tempY;
double tempZ;//Translate anchorPoint to the originGM02.Vector3D tempVec =
new GM02.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-axis
tempX = newPoint.getData(0);tempY = newPoint.getData(1);
newPoint.setData(//new x coordinate0,
tempX*Math.cos(zAngle*Math.PI/180) -tempY*Math.sin(zAngle*Math.PI/180));
newPoint.setData(//new y coordinate1,
tempX*Math.sin(zAngle*Math.PI/180) +tempY*Math.cos(zAngle*Math.PI/180));//Rotate around x-axis
tempY = newPoint.getData(1);tempZ = newPoint.getData(2);
newPoint.setData(//new y coordinate1,
tempY*Math.cos(xAngle*Math.PI/180) -tempZ*Math.sin(xAngle*Math.PI/180));
newPoint.setData(//new z coordinate2,
tempY*Math.sin(xAngle*Math.PI/180) +tempZ*Math.cos(xAngle*Math.PI/180));//Rotate around y-axis
tempX = newPoint.getData(0);tempZ = newPoint.getData(2);
newPoint.setData(//new x coordinate0,
tempX*Math.cos(yAngle*Math.PI/180) +tempZ*Math.sin(yAngle*Math.PI/180));
newPoint.setData(//new z coordinate2,
-tempX*Math.sin(yAngle*Math.PI/180) +tempZ*Math.cos(yAngle*Math.PI/180));//Translate back to anchorPoint
newPoint = 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 GM02.Point3D scale(GM02.ColMatrix3D scale){
return new GM02.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{GM02.ColMatrix2D vector;public Vector2D(GM02.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,GM02.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 GM02.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(((GM02.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 GM02.Vector2D add(GM02.Vector2D vec){return new GM02.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 GM02.Vector2D scale(Double factor){
return new GM02.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 GM02.Vector2D negate(){return new GM02.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 GM02.Vector2D normalize(){
double length = getLength();return new GM02.Vector2D(new ColMatrix2D(
getData(0)/length,getData(1)/length));
}//end normalize//--------------------------------------------------////Computes the dot product of two Vector2D
// objects and returns the result as type double.public double dot(GM02.Vector2D vec){
GM02.ColMatrix2D matrixA = getColMatrix();GM02.ColMatrix2D matrixB = vec.getColMatrix();
return matrixA.dot(matrixB);}//end dot
//--------------------------------------------------////Computes and returns the angle between two Vector2D// objects. The angle is returned in degrees as type
// double.public double angle(GM02.Vector2D vec){
GM02.Vector2D normA = normalize();GM02.Vector2D normB = vec.normalize();
double normDotProd = normA.dot(normB);return Math.toDegrees(Math.acos(normDotProd));
}//end angle//--------------------------------------------------//
}//end class Vector2D//====================================================//
public static class Vector3D{GM02.ColMatrix3D vector;public Vector3D(GM02.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,GM02.Point3D tail){//Get a 2D projection of the tail
GM02.ColMatrix2D tail2D = convert3Dto2D(tail.point);//Get the 3D location of the headGM02.ColMatrix3D head =
tail.point.add(this.getColMatrix());//Get a 2D projection of the headGM02.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 GM02.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(((GM02.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 GM02.Vector3D add(GM02.Vector3D vec){return new GM02.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 GM02.Vector3D scale(Double factor){return new GM02.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 GM02.Vector3D negate(){return new GM02.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 GM02.Vector3D normalize(){double length = getLength();
return new GM02.Vector3D(new ColMatrix3D(getData(0)/length,
getData(1)/length,getData(2)/length));
}//end normalize//--------------------------------------------------////Computes the dot product of two Vector3D
// objects and returns the result as type double.public double dot(GM02.Vector3D vec){
GM02.ColMatrix3D matrixA = getColMatrix();GM02.ColMatrix3D matrixB = vec.getColMatrix();
return matrixA.dot(matrixB);}//end dot
//--------------------------------------------------////Computes and returns the angle between two Vector3D// objects. The angle is returned in degrees as type
// double.public double angle(GM02.Vector3D vec){
GM02.Vector3D normA = normalize();GM02.Vector3D normB = vec.normalize();
double normDotProd = normA.dot(normB);return Math.toDegrees(Math.acos(normDotProd));
}//end angle//--------------------------------------------------//
}//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{GM02.Point2D[] line = new GM02.Point2D[2];public Line2D(GM02.Point2D tail,GM02.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 GM02.ColMatrix2D(
tail.getData(0),tail.getData(1)));this.line[1] = new Point2D(new GM02.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 GM02.Point2D getTail(){
return line[0];
}//end getTail//--------------------------------------------------//public GM02.Point2D getHead(){
return line[1];
}//end getHead//--------------------------------------------------//public void setTail(GM02.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 GM02.ColMatrix2D(
newPoint.getData(0),newPoint.getData(1)));}//end setTail
//--------------------------------------------------//public void setHead(GM02.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 GM02.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{GM02.Point3D[] line = new GM02.Point3D[2];public Line3D(GM02.Point3D tail,GM02.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 GM02.ColMatrix3D(
tail.getData(0),tail.getData(1),
tail.getData(2)));this.line[1] = new Point3D(new GM02.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 GM02.Point3D getTail(){return line[0];}//end getTail
//--------------------------------------------------//public GM02.Point3D getHead(){return line[1];}//end getHead
//--------------------------------------------------//public void setTail(GM02.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 GM02.ColMatrix3D(
newPoint.getData(0),newPoint.getData(1),
newPoint.getData(2)));}//end setTail
//--------------------------------------------------//public void setHead(GM02.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 GM02.ColMatrix3D(newPoint.getData(0),
newPoint.getData(1),newPoint.getData(2)));
}//end setHead//--------------------------------------------------//
public void draw(Graphics2D g2D){//Get 2D projection coordinates.GM02.ColMatrix2D tail =
convert3Dto2D(getTail().point);GM02.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 GM02
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?