This page is optimized for mobile devices, if you would prefer the desktop version just click here

1.3 Game 2302-0110: updating the game math library for graphics  (Page 11/20)

Remember that according to Kjell, a vector doesn't have a position. Hence, there is nothing in the underlying data for a GM2D02.Vector object that specifies a position. In other words, the visual manifestation of avector can be placed anywhere in space, and one placement is just as correct as the next. However, if you become heavily involved in the use of vectors,you will learn that certain placements may be preferable to others in some cases so as to better represent the problem being modeled by the use of vectors.

The draw method of the GM2D02.Vector class

Switching once again to the GM2D02 library, Listing 15 shows the draw method that was added to the GM2D02.Vector class. This method is a little longer than the draw methods that I explained earlier for points and lines. This is mainlybecause:

  • it is necessary to deal with the issue of positioning the visual manifestation of the GM2D02.Vector object, and
  • it is necessary to embellish the drawing to make it possible to visually determine which end is the tail and which end is the head.

This method renders a visual manifestation of a GM2D02.Vector on the specified graphics context, with the tail of the vector located at a pointspecified by the contents of a GM2D02.Point object. A small circle is drawn to visually identify the head of the vector.

Listing 15 . The draw method of the GM2D02.Vector class.
public void draw(Graphics2D g2D,GM2D02.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

Why a circle and not an arrowhead?

When we draw vectors, (particularly when drawing by hand) , we often draw a small arrowhead at the head of the vector. Remember, a vector hasonly two properties: length and direction . The arrowhead normally points in the direction indicated by the direction property. However, there is nothing magic about an arrowhead.Circles are much easier to draw with a computer than arrowheads. As long as you can identify the head of the vector, you can always determine thedirection. Although I didn't give a lot of thought to optimization when developing this library, this is one case where I took the approach that islikely to consume the minimum amount of computational resources.

In Figure 1 , the length of each of the lines in the right image indicates the length property of the respective vector, and the small circle indicates the head. The orientation relative to the current coordinate framerepresents the direction .

Beyond that, further explanation of the code in Listing 15 should not be required.

Three visual manifestations of the same vector

Returning again to the drawOffScreen method of the program named PointLine03 , Listing 16 produces three visual manifestations at three different positions for the same Vector object referred to by the reference variable named vecB . All three of the visual manifestations are colored green in Figure 1 .

<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.