# Connexions

You are here: Home » Content » GAME 2302-0115: Working with Column Matrices, Points, and Vectors

### Recently Viewed

This feature requires Javascript to be enabled.

# GAME 2302-0115: Working with Column Matrices, Points, and Vectors

Module by: R.G. (Dick) Baldwin. E-mail the author

Summary: Learn how to compare column matrices for equality, compare two points for equality, compare two vectors for equality, add one column matrix to another, subtract one column matrix from another, and get a displacement vector from one point to another.

## Preface

This module is one in a series of modules designed for teaching GAME2302 Mathematical Applications for Game Development at Austin Community College in Austin, TX.

What you have learned

In the previous module, you learned how to update the game-math library to provide new capabilities including the addition of graphics and set methods for column matrices, points, vectors, and lines. You also learned how to draw on off-screen images.

What you will learn

In this module, you will learn how to compare column matrices for equality, compare two points for equality, compare two vectors for equality, add one column matrix to another, subtract one column matrix from another, and get a displacement vector from one point to another.

### Viewing tip

I recommend that you open another copy of this module in a separate browser window and use the following links to easily find and view the Images and Listings while you are reading about them.

#### Images

• Image 1 . Screen output from the program named ColMatrixEquals01.
• Image 2 . Screen output from the program named DisplacementVector01.
• Image 3 . Screen output from the program named ColMatrixAddSubtract01.
• Image 4 . Text output from Exercise 1.
• Image 5 . Text output from Exercise 2.
• Image 6 . Text output from Exercise 3.

#### Listings

• Listing 1 . Overridden equals method of the GM2D03.ColMatrix class.
• Listing 2 . Overridden equals method of the GM2D03.Point class.
• Listing 3 . The getColMatrix method of the GM2D03.Point class.
• Listing 4 . Beginning of the ColMatrixEquals01 class.
• Listing 5 . Remainder of the ColMatrixEquals01 class.
• Listing 6 . The getDisplacementVector method of the GM2D03.Point class.
• Listing 7 . The program named DisplacementVector01.
• Listing 8 . Source code for the program named ColMatrixAddSubtract01.
• Listing 9 . Source code for the add method of the GM2D03.ColMatrix class.
• Listing 10 . Source code for the subtract method of the GM2D03.ColMatrix class.
• Listing 11 . Source code for game-math library named GM2D03.
• Listing 12 . Source code for the program named ColMatrixEquals01.
• Listing 13 . Source code for the program named DisplacementVector01.
• Listing 14 . Source code for the program named ColMatrixAddSubtract01.

## Preview

As mentioned earlier, in this module you will learn how to:

• Compare two column matrices for equality.
• Compare two points for equality.
• Compare two vectors for equality.
• Add one column matrix to another.
• Subtract one column matrix from another.
• Get a displacement vector from one point to another.

To assist you in this quest, I will present and explain modifications that were made to update the game-math library that you learned about in the previous two modules. In addition, I will present and explain three sample programs that illustrate the new features of the game-math library.

I will also provide exercises for you to complete on your own at the end of the module. The exercises will concentrate on the material that you have learned in this module and previous modules.

## Discussion and sample code

Much of the code in the library remains unchanged. I explained that code in previous modules and I won't repeat that explanation in this module. Rather, in this module, I will concentrate on explaining the modifications that I made to the library.

### The game-math library named GM2D03

A complete listing of the library program is provided in Listing 11 near the end of the module.

This update added the following new capabilities:

• Compare two ColMatrix objects for equality by implementing Kjell's rules for equality given in his Chapter 1, topic "Column Matrix Equality." The equality test does not test for absolute equality. Rather, It compares the values stored in two matrices and returns true if the values are equal or almost equal and returns false otherwise.
• Get a reference to the ColMatrix object that defines a Point object.
• Compare two Point objects for equality based on a comparison of the ColMatrix objects that define them.
• Get a reference to the ColMatrix object that defines a Vector object.
• Compare two Vector objects for equality based on a comparison of the ColMatrix objects that define them.
• Add one ColMatrix object to a second ColMatrix object, returning a ColMatrix object.
• Subtract one ColMatrix object from a second ColMatrix object, returning a ColMatrix object.
• Get a displacement vector from one Point object to a second Point object. The vector points from the object on which the getDisplacementVector method is called to the object passed as a parameter to the method.

I will explain these updates in conjunction with the discussions of the programs that follow.

### The sample program named ColMatrixEquals01

A complete listing of this program is provided in Listing 12 near the end of the module. I will explain the program in fragments. In selecting the portions of the program that I will explain, I will skip over material that is very similar to code that I have previously explained.

The purpose of this program is to confirm the behavior of the equals methods of the GM2D03.ColMatrix , Point , and Vector classes.

Overridden equals method of the GM2D03.ColMatrix class

I will begin by explaining some of the equals methods in the updated GM2D03 game-math library.

The first fragment in Listing 1 shows the new equals method of the ColMatrix class.

1
Listing 1: Overridden equals method of the GM2D03.ColMatrix class.
    public boolean equals(Object obj){
if(obj instanceof GM2D03.ColMatrix &&
Math.abs(((GM2D03.ColMatrix)obj).getData(0) -
getData(0)) <= 0.00001 &&
Math.abs(((GM2D03.ColMatrix)obj).getData(1) -
getData(1)) <= 0.00001){
return true;
}else{
return false;
}//end else

}//end overridden equals method


This method overrides the equals method inherited from the Object class. It compares the double values stored in two matrices and returns true if the values are equal or almost equal and returns false otherwise.

An inherent problem when comparing doubles and floats

There is always a problem when comparing two double or float values for equality. If you perform a series of computations twice, using a different computational order for each set of computations, you are likely to end up with two values that are not absolutely equal even if they should be equal. Arithmetic inaccuracies along the way may cause the two results to differ ever so slightly. However, they may be equal from a practical viewpoint.

#### Note:

An example of the problem:

See Image 3 where the a value that should be 0.0 is actually given by -1.7763568394002505E-15

Therefore, when comparing two double or float values for equality, it is customary to subtract one from the other, convert the difference to an absolute value, and compare that absolute value with an arbitrarily small positive value. If the difference is less than that the test value, the two original values are declared to be equal. Otherwise, they are declared to be unequal.

This is the logic that is implemented in Listing 1 , which shouldn't require further explanation.

Overridden equals method of the GM2D03.Point class

Listing 2 presents the overridden equals method of the GM2D03.Point class.

2
Listing 2: Overridden equals method of the GM2D03.Point class.
    public boolean equals(Object obj){
if(point.equals(((GM2D03.Point)obj).
getColMatrix())){
return true;
}else{
return false;
}//end else

}//end overridden equals method


This method also overrides the equals method inherited from the Object class. It compares the values stored in the ColMatrix objects that define two Point objects and returns true if they are equal and false otherwise.

One possible point of confusion

The one thing that can be confusing in Listing 2 has to do with the variable named point . This is a reference to an object of type ColMatrix that defines the location of the Point object. Listing 2 calls another new method named getColMatrix to get access to the ColMatrix object that defines the incoming Point object, and uses that object for the comparison. In effect, this method actually compares two objects of the ColMatrix class by calling the method that I explained in Listing 1 . This illustrates the advantages of building up the library classes using objects of the fundamental ColMatrix class.

The getColMatrix method of the GM2D03.Point class

This extremely simple new method, which is called by the code in Listing 2 , is presented in Listing 3 .

3
Listing 3: The getColMatrix method of the GM2D03.Point class.
    //Returns a reference to the ColMatrix object that
// defines this Point object.
public GM2D03.ColMatrix getColMatrix(){
return point;
}//end getColMatrix


Listing 3 shouldn't require any explanation beyond the embedded comments.

Overridden equals method of the GM2D03.Vector class

The overridden equals method of the GM2D03.Vector class is essentially the same as the code shown for the Point class in Listing 2 so I won't bother to show and explain it. You van view this new method in Listing 11 .

Beginning of the ColMatrixEquals01 program class

At this point, I will put the discussion of the updated GM2D03 library on hold and explain the ColMatrixEquals01 program.

Listing 4 presents the beginning of the ColMatrixEquals01 program class including the beginning of the main method.

4
Listing 4: Beginning of the ColMatrixEquals01 class.
public class ColMatrixEquals01{
public static void main(String[] args){
GM2D03.ColMatrix matA =
new GM2D03.ColMatrix(1.5,-2.6);
GM2D03.ColMatrix matB =
new GM2D03.ColMatrix(1.5,-2.6);
GM2D03.ColMatrix matC =
new GM2D03.ColMatrix(1.500001,-2.600001);
GM2D03.ColMatrix matD =
new GM2D03.ColMatrix(1.50001,-2.60001);
System.out.println(matA.equals(matA));
System.out.println(matA.equals(matB));
System.out.println(matA.equals(matC));
System.out.println(matA.equals(matD));


Listing 4 instantiates four different ColMatrix objects and then compares them in different ways, displaying the results of the comparisons on the command-line screen.

Screen output from the program named ColMatrixEquals01

The first four lines of text in Image 1 were produced by the code in Listing 4. (The remaining output shown in Image 1 was produced by the code in Listing 5 , which I will explain shortly.)

5
Image 1: Screen output from the program named ColMatrixEquals01.
true
true
true
false

true
true
false

true
true
false


Because of the simplicity of the code in Listing 4 , you shouldn't need any help in understanding why the code in Listing 4 produced the first four lines of output in Image 1 .

The third and fourth lines of output in Image 1 are the result of comparing two matrices whose values are almost equal but not absolutely equal.

Remainder of the ColMatrixEquals01 class

The remainder of the program named ColMatrixEquals01 is shown in Listing 5 . The output produced by this code is shown in the last six lines of text in Image 1 .

6
Listing 5: Remainder of the ColMatrixEquals01 class.
    GM2D03.Point pointA = new GM2D03.Point(
new GM2D03.ColMatrix(15.6,-10.11));
GM2D03.Point pointB = new GM2D03.Point(
new GM2D03.ColMatrix(15.6,-10.11));
GM2D03.Point pointC = new GM2D03.Point(
new GM2D03.ColMatrix(-15.6,10.11));
System.out.println(/*Blank line*/);
System.out.println(pointA.equals(pointA));
System.out.println(pointA.equals(pointB));
System.out.println(pointA.equals(pointC));

GM2D03.Vector vecA = new GM2D03.Vector(
new GM2D03.ColMatrix(15.6,-10.11));
GM2D03.Vector vecB = new GM2D03.Vector(
new GM2D03.ColMatrix(15.6,-10.11));
GM2D03.Vector vecC = new GM2D03.Vector(
new GM2D03.ColMatrix(-15.6,10.11));
System.out.println(/*Blank line*/);
System.out.println(vecA.equals(vecA));
System.out.println(vecA.equals(vecB));
System.out.println(vecA.equals(vecC));

}//end main
}//end ColMatrixEquals01 class


Once again, the code in Listing 5 is very straightforward and shouldn't require further explanation. The screen output shown in Image 1 verifies that the library methods called by this program behave appropriately.

### The sample program named DisplacementVector01

A displacement vector describes the distance and direction that you would have to move to get from one point in space to another point in space.

The getDisplacementVector method of the GM2D03.Point class

Returning to the discussion of the updated GM2D03 library, Listing 6 presents the new getDisplacementVector method of the GM2D03.Point class

7
Listing 6: The getDisplacementVector method of the GM2D03.Point class.
    public GM2D03.Vector getDisplacementVector(
GM2D03.Point point){
return new GM2D03.Vector(new GM2D03.ColMatrix(
point.getData(0)-getData(0),
point.getData(1)-getData(1)));
}//end getDisplacementVector


This method gets and returns a displacement vector from one Point object to a second Point object and returns the result as a reference to a new object of the class GM2D03.Vector .

The direction of the vector

The displacement vector points from the Point object on which the method is called to the Point object passed as a parameter to the method. Kjell describes the component parts of the new vector as the distance you would have to walk, first along the x-axis and then along the y-axis to get from the first point to the second point. Of course, you could take the short cut and walk directly from the first point to the second point but that's often not how we do it in programming.

The code in Listing 6 is straightforward and shouldn't require further explanation.

The program named DisplacementVector01

Once again, I will put the discussion of the updated GM2D03 library on hold and explain the program named DisplacementVector01 .

Listing 7 shows the program named DisplacementVector01 in its entirety.

(For convenience, a second copy of this program is provided in Listing 13 near the end of the module along with the other three programs discussed in this module.)

8
Listing 7: The program named DisplacementVector01.
public class DisplacementVector01{
public static void main(String[] args){
GM2D03.Point pointA = new GM2D03.Point(
new GM2D03.ColMatrix(6.5,-9.7));
GM2D03.Point pointB = new GM2D03.Point(
new GM2D03.ColMatrix(-6.0,9.0));

System.out.println(pointA.getDisplacementVector(
pointB));
System.out.println(pointB.getDisplacementVector(
pointA));

}//end main
}//end DisplacementVector01 class


The purpose of this program is to confirm the behavior of the getDisplacementVector method of the GM2D03.Point class. The screen output shown in Image 2 provides that confirmation.

Screen output from the program named DisplacementVector01

The screen output from the program named DisplacementVector01 is shown in Image 2 .

9
Image 2: Screen output from the program named DisplacementVector01.
-12.5,18.7
12.5,-18.7


You should be able to correlate the results shown in Image 2 with the code in Listing 6 and Listing 7 without further explanation.

Very simple methods

By now you may be thinking that the code in the game-math library is very simple and easy to understand. I hope that is the case. I went to great lengths to modularize the library into a set of simple and easily understood methods. Taken as a whole, however, the library is becoming quite powerful, and will become even more powerful as we progress through additional modules in this collection.

### The sample program named ColMatrixAddSubtract01

The source code for this program is shown in its entirety in Listing 8 .

(The source code is also provided in Listing 14 near the end of the module for convenience.)

10
Listing 8: Source code for the program named ColMatrixAddSubtract01.
public class ColMatrixAddSubtract01{
public static void main(String[] args){

GM2D03.ColMatrix matrixA =
new GM2D03.ColMatrix(3.14,-6.01);
GM2D03.ColMatrix matrixB =
new GM2D03.ColMatrix(-14.0,-12.2);

GM2D03.ColMatrix matrixD = matrixC.subtract(matrixA);
GM2D03.ColMatrix matrixE = matrixD.subtract(matrixB);

System.out.println(matrixC);
System.out.println(matrixD);
System.out.println(matrixE);
}//end main



The purpose of this program is to confirm the behavior of the new add and subtract methods of the GM2D03.ColMatrix class.

Source code for the add method of the GM2D03.ColMatrix class

Returning once more to a discussion of the updated GM2D03 library, the source code for this method is shown in Listing 9 . The method adds one ColMatrix object to another ColMatrix object, returning a ColMatrix object. As you should have learned from your studies of the Kjell tutorial, the order in which the two objects are added doesn't matter. The result is the same either way.

11
Listing 9: Source code for the add method of the GM2D03.ColMatrix class.
    public GM2D03.ColMatrix add(GM2D03.ColMatrix matrix){
return new GM2D03.ColMatrix(
getData(0)+matrix.getData(0),
getData(1)+matrix.getData(1));


The code in the method is straightforward and shouldn't require an explanation.

Source code for the subtract method of the GM2D03.ColMatrix class

Continuing with the discussion of the updated GM2D03 library, the source code for this method is shown in Listing 10 . This method subtracts one ColMatrix object from another ColMatrix object, returning a ColMatrix object. Also as you should have learned from the Kjell tutorial, in this case, the order of the operation does matter. The object that is received as an incoming parameter is subtracted from the object on which the method is called. Reversing the order of operations produces different results.

12
Listing 10: Source code for the subtract method of the GM2D03.ColMatrix class.
    public GM2D03.ColMatrix subtract(
GM2D03.ColMatrix matrix){
return new GM2D03.ColMatrix(
getData(0)-matrix.getData(0),
getData(1)-matrix.getData(1));
}//end subtract


Once again, the code in the method is straightforward and shouldn't require an explanation.

Program output for program named ColMatrixAddSubtract01

Now, lets get back to the program named ColMatrixAddSubtract01 that is shown in Listing 8 . This program produces the output shown in Image 3 .

13
Image 3: Screen output from the program named ColMatrixAddSubtract01.
-10.86,-18.21
-14.0,-12.200000000000001
0.0,-1.7763568394002505E-15


By now, you should have no difficulty understanding how the output shown in Image 3 was produced by the code shown in Listing 8 . Note, however, that the last value on the third row should be 0.0 instead of the extremely small value shown. This is an example of an inherent problem having to do with comparing double or float values with other double or float values for absolute equality.

## Documentation for the GM2D03 library

Click here to download a zip file containing standard javadoc documentation for the library named GM2D03 . Extract the contents of the zip file into an empty folder and open the file named index.html in your browser to view the documentation.

Although the documentation doesn't provide much in the way of explanatory text (see Listing 11 and the explanations given above) , the documentation does provide a good overview of the organization and structure of the library. You may find it helpful in that regard.

## Homework assignment

Your homework assignment for this module was to study Kjell's tutorial through Chapter 2 - Column and Row Matrix Addition .

The homework assignment for the next module is to study the Kjell tutorial through Chapter 3 - Vector Addition .

In addition to studying the Kjell material, you should read at least the next two modules in this collection and bring your questions about that material to the next classroom session.

Finally, you should have begun studying the physics material at the beginning of the semester and you should continue studying one physics module per week thereafter. You should also feel free to bring your questions about that material to the classroom for discussion.

## Run the programs

I encourage you to copy the code from Listing 12 , Listing 13 , and Listing 14 . Compile the code and execute it in conjunction with the game-math library provided in Listing 11 . Experiment with the code, making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

## Summary

In this module, you learned how to compare column matrices for equality, compare two points for equality, compare two vectors for equality, add one column matrix to another, subtract one column matrix from another, and get a displacement vector from one point to another.

## What's next?

The next module in the collection will show one way for you to visualize column matrices in graphical form.

In the module following that one, you will learn:

• How to add two or more vectors.
• About the relationship between the length of the sum of vectors and the sum of the lengths of the vectors.
• How to add a vector to a point.
• How to get the length of a vector.
• How to represent an object in different coordinate frames.

## Miscellaneous

This section contains a variety of miscellaneous information.

### Note:

Housekeeping material
• Module name: GAME2302-0115: Working with Column Matrices, Points, and Vectors
• File: Game0115.htm
• Published: 10/14/12
• Revised: 12/31/12

### Note:

Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

## Complete program listings

Complete listings of the programs discussed in this module are shown in Listing 11 through Listing 14 below.

14
Listing 11: Source code for game-math library named GM2D03.
/*GM2D03.java
Revised 02/10/08

The name GM2Dnn is an abbreviation for GameMath2Dnn.

See the file named GM2D01.java for a general description
of this game-math library file. This file is an update of
GM2D02.

This update added the following new capabilities:

Compare two ColMatrix objects for equality by implementing
Kjell's rules for equality given in his Chapter 1, topic
"Column Matrix Equality." The equality test does not test
for absolute equality.  Rather, It compares the values
stored in two matrices and returns true if the values are
equal or almost equal and returns false otherwise.

Get a reference to the ColMatrix object that defines
a Point object.

Compare two Point objects for equality based on a
comparison of the ColMatrix objects that define them.

Get a reference to the ColMatrix object that defines
a Vector object.

Compare two Vector objects for equality based on a
comparison of the ColMatrix objects that define them.

Add one ColMatrix object to a second ColMatrix object,
returning a ColMatrix object.

Subtract one ColMatrix object from a second ColMatrix
object, returning a ColMatrix object.

Get a displacement vector from one Point object to a
second Point object. The vector points from the object
on which the getDisplacementVector method is called to the
object passed as a parameter to the method.

Tested using JDK 1.6 under WinXP.
*********************************************************/
import java.awt.geom.*;
import java.awt.*;

public class GM2D03{

//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 ColMatrix{
double[] data = new double[2];

public ColMatrix(double data0,double data1){
data[0] = data0;
data[1] = data1;
}//end constructor
//--------------------------------------------------//

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 GM2D03.ColMatrix &&
Math.abs(((GM2D03.ColMatrix)obj).getData(0) -
getData(0)) <= 0.00001 &&
Math.abs(((GM2D03.ColMatrix)obj).getData(1) -
getData(1)) <= 0.00001){
return true;
}else{
return false;
}//end else

}//end overridden equals method
//--------------------------------------------------//

//Adds one ColMatrix object to another ColMatrix
// object, returning a ColMatrix object.
return new GM2D03.ColMatrix(
getData(0)+matrix.getData(0),
getData(1)+matrix.getData(1));
//--------------------------------------------------//

//Subtracts one ColMatrix object from another
// ColMatrix object, returning a ColMatrix object. The
// object that is received as an incoming parameter
// is subtracted from the object on which the method
// is called.
public GM2D03.ColMatrix subtract(
GM2D03.ColMatrix matrix){
return new GM2D03.ColMatrix(
getData(0)-matrix.getData(0),
getData(1)-matrix.getData(1));
}//end subtract
//--------------------------------------------------//
}//end class ColMatrix
//====================================================//

public static class Point{
GM2D03.ColMatrix point;

public Point(GM2D03.ColMatrix point){//constructor
//Create and save a clone of the ColMatrix object
// used to define the point to prevent the point
// from being corrupted by a later change in the
// values stored in the original ColVector object
// through use of its set method.
this.point =
new ColMatrix(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){
Ellipse2D.Double circle =
new Ellipse2D.Double(getData(0)-3,
getData(1)-3,
6,
6);
g2D.draw(circle);
}//end draw
//--------------------------------------------------//

//Returns a reference to the ColMatrix object that
// defines this Point object.
public GM2D03.ColMatrix getColMatrix(){
return point;
}//end getColMatrix
//--------------------------------------------------//

//This method overrides the equals method inherited
// from the class named Object. It compares the values
// stored in the ColMatrix objects that define two
// Point objects and returns true if they are equal
// and false otherwise.
public boolean equals(Object obj){
if(point.equals(((GM2D03.Point)obj).
getColMatrix())){
return true;
}else{
return false;
}//end else

}//end overridden equals method
//--------------------------------------------------//

//Gets a displacement vector from one Point object to
// a second Point 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 GM2D03.Vector getDisplacementVector(
GM2D03.Point point){
return new GM2D03.Vector(new GM2D03.ColMatrix(
point.getData(0)-getData(0),
point.getData(1)-getData(1)));
}//end getDisplacementVector
//--------------------------------------------------//
}//end class Point
//====================================================//

public static class Vector{
GM2D03.ColMatrix vector;

public Vector(GM2D03.ColMatrix vector){//constructor
//Create and save a clone of the ColMatrix object
// used to define the vector to prevent the vector
// from being corrupted by a later change in the
// values stored in the original ColVector object.
this.vector = new ColMatrix(
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 circle at the
public void draw(Graphics2D g2D,GM2D03.Point tail){
Line2D.Double line = new Line2D.Double(
tail.getData(0),
tail.getData(1),
tail.getData(0)+vector.getData(0),
tail.getData(1)+vector.getData(1));

//Draw a small circle to identify the head.
Ellipse2D.Double circle = new Ellipse2D.Double(
tail.getData(0)+vector.getData(0)-2,
tail.getData(1)+vector.getData(1)-2,
4,
4);
g2D.draw(circle);
g2D.draw(line);
}//end draw
//--------------------------------------------------//

//Returns a reference to the ColMatrix object that
// defines this Vector object.
public GM2D03.ColMatrix getColMatrix(){
return vector;
}//end getColMatrix
//--------------------------------------------------//

//This method overrides the equals method inherited
// from the class named Object. It compares the values
// stored in the ColMatrix objects that define two
// Vector objects and returns true if they are equal
// and false otherwise.
public boolean equals(Object obj){
if(vector.equals((
(GM2D03.Vector)obj).getColMatrix())){
return true;
}else{
return false;
}//end else

}//end overridden equals method
//--------------------------------------------------//
}//end class Vector
//====================================================//

//A line is defined by two points. One is called the
// tail and the other is called the head.
public static class Line{
GM2D03.Point[] line = new GM2D03.Point[2];

//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 Point(new GM2D03.ColMatrix(
tail.getData(0),tail.getData(1)));
this.line[1] = new Point(new GM2D03.ColMatrix(
}//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 GM2D03.Point getTail(){
return line[0];
}//end getTail
//--------------------------------------------------//

return line[1];
//--------------------------------------------------//

public void setTail(GM2D03.Point 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 Point(new GM2D03.ColMatrix(
newPoint.getData(0),newPoint.getData(1)));
}//end setTail
//--------------------------------------------------//

//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 Point(new GM2D03.ColMatrix(
newPoint.getData(0),newPoint.getData(1)));
//--------------------------------------------------//

public void draw(Graphics2D g2D){
Line2D.Double line = new Line2D.Double(
getTail().getData(0),
getTail().getData(1),
g2D.draw(line);
}//end draw
//--------------------------------------------------//
}//end class Line
//====================================================//

}//end class GM2D03


.

15
Listing 12: Source code for the program named ColMatrixEquals01.
/*ColMatrixEquals01.java
Revised 02/08/08

The purpose of this program is to confirm the behavior of
the equals methods of the GM2D03.ColMatrix, Point, and
Vector classes.

Tested using JDK 1.6 under WinXP.
*********************************************************/

public class ColMatrixEquals01{
public static void main(String[] args){
GM2D03.ColMatrix matA =
new GM2D03.ColMatrix(1.5,-2.6);
GM2D03.ColMatrix matB =
new GM2D03.ColMatrix(1.5,-2.6);
GM2D03.ColMatrix matC =
new GM2D03.ColMatrix(1.500001,-2.600001);
GM2D03.ColMatrix matD =
new GM2D03.ColMatrix(1.50001,-2.60001);
System.out.println(matA.equals(matA));
System.out.println(matA.equals(matB));
System.out.println(matA.equals(matC));
System.out.println(matA.equals(matD));

GM2D03.Point pointA = new GM2D03.Point(
new GM2D03.ColMatrix(15.6,-10.11));
GM2D03.Point pointB = new GM2D03.Point(
new GM2D03.ColMatrix(15.6,-10.11));
GM2D03.Point pointC = new GM2D03.Point(
new GM2D03.ColMatrix(-15.6,10.11));
System.out.println(/*Blank line*/);
System.out.println(pointA.equals(pointA));
System.out.println(pointA.equals(pointB));
System.out.println(pointA.equals(pointC));

GM2D03.Vector vecA = new GM2D03.Vector(
new GM2D03.ColMatrix(15.6,-10.11));
GM2D03.Vector vecB = new GM2D03.Vector(
new GM2D03.ColMatrix(15.6,-10.11));
GM2D03.Vector vecC = new GM2D03.Vector(
new GM2D03.ColMatrix(-15.6,10.11));
System.out.println(/*Blank line*/);
System.out.println(vecA.equals(vecA));
System.out.println(vecA.equals(vecB));
System.out.println(vecA.equals(vecC));

}//end main
}//end ColMatrixEquals01 class


.

16
Listing 13: Source code for the program named DisplacementVector01.
/*DisplacementVector01.java
Revised 02/08/08

The purpose of this program is to confirm the behavior of
the getDisplacementVector method of the GM2D03.Point
class.

Tested using JDK 1.6 under WinXP.
*********************************************************/

public class DisplacementVector01{
public static void main(String[] args){
GM2D03.Point pointA = new GM2D03.Point(
new GM2D03.ColMatrix(6.5,-9.7));
GM2D03.Point pointB = new GM2D03.Point(
new GM2D03.ColMatrix(-6.0,9.0));

System.out.println(pointA.getDisplacementVector(
pointB));
System.out.println(pointB.getDisplacementVector(
pointA));

}//end main
}//end DisplacementVector01 class


.

17
Listing 14: Source code for the program named ColMatrixAddSubtract01.
/*ColMatrixAddSubtract01.java
Revised 02/08/08

The purpose of this program is to confirm the behavior of
the add and subtract methods of the GM2D03.ColMatrix
class.

Tested using JDK 1.6 under WinXP.
*********************************************************/

public static void main(String[] args){

GM2D03.ColMatrix matrixA =
new GM2D03.ColMatrix(3.14,-6.01);
GM2D03.ColMatrix matrixB =
new GM2D03.ColMatrix(-14.0,-12.2);

GM2D03.ColMatrix matrixD = matrixC.subtract(matrixA);
GM2D03.ColMatrix matrixE = matrixD.subtract(matrixB);

System.out.println(matrixC);
System.out.println(matrixD);
System.out.println(matrixE);
}//end main



## Exercises

### Exercise 1

Using Java and the game-math library named GM2D03 , or using a different programming environment of your choice, write a program that creates four column matrix objects using the following names and values:

• matA = (1.5,-2.6)
• matB = (1.5,-2.6)
• matC = (8.5,-13.4)
• matD = (5.5,-8.2)

Then use matrix addition and subtraction to compute and display the values of the following matrices:

• matE = matA + matB
• matF = matC - matD

Finally, test matE and matF for equality and display the result, replacing the question mark with either true or false..

Cause the program to display your name in some manner.

My version of the program produces the text output shown in Image 4 , and you should use that as a display template.

18

Prof. Baldwin
matE = 3.0,-5.2
matF = 3.0,-5.200000000000001
matE equals matF: ?


### Exercise 2

Using Java and the game-math library named GM2D03 , or using a different programming environment of your choice, write a program that creates and displays (in text form) three vectors having the names and values shown in Image 5 .

Cause your program to test vecA for equality, first against vecB , and then against vecC , and display the results of the tests in place of the question marks shown in Image 5 .

19
Image 5: Text output from Exercise 2.

Prof. Baldwin
vecA = -1.5,2.6
vecB = -1.5,2.6
vecC = 8.5,-13.4
vecA equals vecB: ?
vecA equals vecC: ?


### Exercise 3

Using Java and the game-math library named GM2D03 , or using a different programming environment of your choice, write a program that creates and displays four column matrices having the names and values shown in Image 6 .

Use the first two matrices to create a displacement vector named dispVecE and display its value in place of the question marks in Image 6 .

Use the last two matrices to create another displacement vector named dispVecF and display its value in place of the question marks in Image 6 .

Test dispVecE and dispVecF for equality and display the result of the test in place of the final question mark in Image 6 .

20
Image 6: Text output from Exercise 3.

Prof. Baldwin
matA = 1.5,-2.6
matB = -1.5,2.6
dispVecE = ?,?
matC = 8.5,-13.4
matD = 5.5,-8.2
dispVecF = ?,?
dispVecE equals dispVecF: ?


-end-

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks