<< Chapter < Page Chapter >> Page >

The getData method of the Vector class knows nothing about position. That method simply returns the X and Y coordinate values that describethe length of the vector relative to its tail.

(For the special case where the tail is located at the origin, the getData method returns the X and Y coordinates of the head of the vector.)

However, because the tail of the red vector in this case is not necessarily located at the origin, we must calculate the position of the head of the redvector taking the position of its tail ( startPoint ) into account. The code in Listing 16 does just that.

That concludes the discussion of the program named CoordinateFrame01 .

The program named VectorAdd02

Hopefully, you have been studying the Kjell tutorial as instructed in the section titled Homework assignment . This program illustrates the addition of two vectors using two different approachesthat result in the parallelogram described by Kjell. That parallelogram is shown in the screen output in Figure 4 .

Figure 4 Screen output from the program named VectorAdd02.

Missing image.

The method named drawOffScreen

A complete listing of this program is provided in Listing 22 near the end of the module. Most of the new material is contained in the method named drawOffScreen . Therefore, I will limit my explanation to the method named drawOffScreen , which begins in Listing 17 .

Listing 17 . Beginning of the method named drawOffScreen of the program named VectorAdd02.
void drawOffScreen(Graphics2D g2D){//Translate the origin to a position near the bottom- // left corner of the off-screen image and draw a pair// of orthogonal axes that intersect at the origin. setCoordinateFrame(g2D);//Define two vectors.GM2D04.Vector vecA = new GM2D04.Vector(new GM2D04.ColMatrix(50,-100)); GM2D04.Vector vecB = new GM2D04.Vector(new GM2D04.ColMatrix(75,-25));//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)));//Draw vecB in GREEN with its tail at the head of vecAg2D.setColor(Color.GREEN); vecB.draw(g2D,new GM2D04.Point(new GM2D04.ColMatrix(vecA.getData(0),vecA.getData(1))));//Define a third vector as the sum of the first // two vectors defined above by adding vecB to vecA.GM2D04.Vector sumA = vecA.add(vecB);//Draw sumA in BLACK with its tail at the origin. // The head will coincide with the head of the// green vecB. g2D.setColor(Color.BLACK);sumA.draw(g2D,new GM2D04.Point( new GM2D04.ColMatrix(0.0,0.0)));

There is nothing new in Listing 17 . The code in Listing 17 produces the black vector in Figure 4 plus the red and green vectors that appear above the black vector.

Do the same operations in a different order

Listing 18 begins by drawing the red and green vectors again. However, this time the green vector is drawn with its tail at the origin, and the red vectoris drawn with its tail at the head of the green vector as shown by the green and red vectors below the black vector in Figure 4 . This is perfectly legal because a vector has no location property. We can draw a vector anywhere we pleaseprovided we draw it with the correct length and direction.

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