<< Chapter < Page Chapter >> Page >

Coding for clarity

Coding for clarity:

Ordinarily I would compress those two statements into a single statement by instantiating an anonymous object of the ColMatrix class in the call to the constructor for the Point class, and I recommend that you do the same if you know how. However, I elected to separate the code into two statements in thiscase to provide clarity and make it somewhat easier for you to understand.

Display the values

Following that, the code in Listing 10 displays the coordinate values that represent the point in the same two ways described earlier for the ColMatrix object. The screen output is shown in Figure 5 .

The GM2D01.Point class

The complete definition of the static top-level class named Point is shown in Listing 11 .

Listing 11 . The static top-level class named Point.
//A fragment from the GM2D01 class public static class Point{GM2D01.ColMatrix point;Point(GM2D01.ColMatrix point){ this.point = point;}//end constructorpublic String toString(){ return point.getData(0) + "," + point.getData(1);}//end toStringpublic double getData(int index){ if((index<0) || (index>1)) throw new IndexOutOfBoundsException();return point.getData(index); }//end getData}//end class Point

Less detail in the discussions

By now, you may be getting a little bored with the detail in which I have been discussing and explaining the code so I will be more brief in myexplanations from here on.

The code in Listing 11 is straightforward and shouldn't require a lot of explanation. Perhaps the most significant thing to note about Listing 11 is that the coordinate values that represent the point are actually stored internally inan object of the type ColMatrix . That approach will prove to be convenient for certain mathematical operations that will be explained in futuremodules.

The GM2D01.Vector class

The static top-level class named Vector is shown in Listing 12 .

(Note that this is a different class from the class named java.util.Vector in the standard Java library.)

You will find the code that exercises this class and produces the output shown in Figure 5 in the complete listing of the program named PointLine02 in Listing 15 . That code is straightforward and shouldn't require an explanation.

Listing 12 . The static top-level class named Vector.
//A fragment from the GM2D01 class public static class Vector{GM2D01.ColMatrix vector;Vector(GM2D01.ColMatrix vector){ this.vector = vector;}//end constructorpublic String toString(){ return vector.getData(0) + "," + vector.getData(1);}//end toStringpublic double getData(int index){ if((index<0) || (index>1)) throw new IndexOutOfBoundsException();return vector.getData(index); }//end getData}//end class Vector

Storing a vector in a column matrix

Recall that Kjell tells us that both points and vectors can be conveniently stored in a column matrix. As a result, thecode in Listing 12 , (at this stage in the development of the library) , is essentially the same as the code for the Point class in Listing 11 . The only differences are a few differences in names.

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