<< Chapter < Page Chapter >> Page >

The method named setCoordinateFrame

This method sets the origin to a point near the upper-left corner of the off-screen image (see Figure 1 ) and draws orthogonal axes on the off-screen image intersecting at the origin.

Listing 6 . The method named setCoordinateFrame.
private void setCoordinateFrame(Graphics2D g2D){ //Translate the origin.g2D.translate(0.2*osiWidth,0.2*osiHeight);//Draw new X and Y-axes in default BLACK g2D.drawLine(-(int)(0.2*osiWidth),0,(int)(0.8*osiWidth),0);g2D.drawLine(0,-(int)(0.2*osiHeight), 0,(int)(0.8*osiHeight));}//end setCoordinateFrame method

There is no intention to perform mathematical operations on the axes, so they are drawn independently of the classes and methods in the game-mathlibrary using the simplest method available for drawing a line.

The name of that simple method is drawLine , and it is a method of the standard Java Graphics class. The translate method is also a method of the Graphics class. Given that information, the code in Listing 6 is straightforward and should not require further explanation.

Adding two vectors

Returning to the method named drawOffScreen , Listing 7 begins by instantiating two objects of the GM2D04.Vector class.

Listing 7 . Adding two vectors.
GM2D04.Vector vecA = new GM2D04.Vector( new GM2D04.ColMatrix(50,100));GM2D04.Vector vecB = new GM2D04.Vector( new GM2D04.ColMatrix(75,25));GM2D04.Vector sumOf2 = vecA.add(vecB);

Then Listing 7 calls the new add method (see Listing 1 ) on one of the vectors, passing the other vector as a parameter to the method.The add method returns a third vector that is the sum of the other two vectors. The new vector is referred to as sumOf2 in Listing 7 .

Draw vecA in RED with its tail at the origin

Recall that a vector has only two properties: length and direction. It does not have a position property. Therefore, if you decide to draw a vector, you candraw it anywhere in space, and one position is equally as valid as any other position.

Listing 8 sets the drawing color to RED and calls the draw method on vecA producing the red visual manifestation of the Vector object shown in Figure 1 .

(Note that there is also a magenta vector in Figure 1 , and it may be difficult to distinguish from the red vector, depending on the quality of thecolor on your monitor. The magenta vector is longer than the red vector.)

Listing 8 . Draw vecA in RED with its tail at the origin.
g2D.setColor(Color.RED); vecA.draw(g2D,new GM2D04.Point(new GM2D04.ColMatrix(0,0)));

Note that I elected to draw the vector with its tail at the origin, but that was an arbitrary decision that I will discuss in more detail later.

Draw vecB in GREEN head-to-tail with vecA

While it's legal to draw a vector anywhere in space, certain positions may have advantages over other positions in some cases. You will see what I meanshortly. In the meantime, Listing 9 creates a visual manifestation of the vecB object with its tail at the head of the vecA object as shown by the green vector in Figure 1 .

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