<< Chapter < Page Chapter >> Page >
Listing 12 . Draw six lines connecting the vertices of the hexagon.
GM2D02.Line line01 = new GM2D02.Line(point0,point1); GM2D02.Line line12 = new GM2D02.Line(point1,point2);GM2D02.Line line23 = new GM2D02.Line(point2,point3); GM2D02.Line line34 = new GM2D02.Line(point3,point4);GM2D02.Line line45 = new GM2D02.Line(point4,point5); GM2D02.Line line50 = new GM2D02.Line(point5,point0);//Now draw a visual manifestation of each line // on g2Da.line01.draw(g2Da); line12.draw(g2Da);line23.draw(g2Da); line34.draw(g2Da);line45.draw(g2Da); line50.draw(g2Da);

Then Listing 12 calls the draw method belonging to the GM2D02.Line class six times in succession to cause visible lines to be rendered on the specified off-screen image. You can see those sixlines in the left image in Figure 1 .

That completes the drawing that is performed on the off-screen image shown in the left image in Figure 1 . Next I will explain the drawing that is performed on the off-screen image that is shown as the right image in Figure 1 .

Instantiate three objects of type GM2D02.Vector

According to Kjell, "A vector is a geometrical object that has two properties: length and direction ." He also tells us, "A vector does not have a position."

The right image in Figure 1 shows visual manifestations of three differentobjects of the GM2D02.Vector class. One vector is represented as a red line with a small red circle at its head. A second vector isrepresented as a blue line with a small blue circle at its head. The third vector is shown in three different positions represented by green lines withsmall green circles at their heads.

Listing 13 instantiates three objects of the GM2D02.Vector class, which are visually represented by the red, green, and blue lines in Figure 1 . References to these three objects are saved in the variables named vecA , vecB , and vecC in Listing 13 .

Listing 13 . Instantiate three objects of type GM2D02.Vector.
GM2D02.Vector vecA = new GM2D02.Vector( new GM2D02.ColMatrix(50,100));GM2D02.Vector vecB = new GM2D02.Vector( new GM2D02.ColMatrix(75,25));GM2D02.Vector vecC = new GM2D02.Vector( new GM2D02.ColMatrix(125,125));

Note that each vector is described by two values in a GM2D02.ColMatrix object.

Call the draw method to draw the vector

Listing 14 begins by setting the drawing color to red for the off-screen image on which the visual manifestation of the first Vector object will be drawn. Then Listing 14 calls the draw method of the GM2D02.Vector class to cause the object referred to by vecA to be represented as a (red) line with its tail at the origin. (Unlike the image on the left side of Figure 1 , the origin for the image on the right was not translated to the center.)

Listing 14 . Call the draw method to draw the vector.
g2Db.setColor(Color.RED);vecA.draw(g2Db,new GM2D02.Point( new GM2D02.ColMatrix(0,0)));

Note that two parameter are passed to the draw method in Listing 14:

  • A reference to the off-screen graphics context on which the visual manifestation of the vector will be drawn.
  • A new object of the class GM2D02.Point that will be used to determine the position on the off-screen image in which the visualmanifestation will appear.

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