<< Chapter < Page Chapter >> Page >

In addition, the GUI provides a 2D drawing area. When the user clicks the OK button, the program draws the two vectors, (one in black and the other in magenta) , on the output screen with the tail of each vector located at the origin in 2D space. The program also displays the values ofthe dot product of the two vectors and the angle between the two vectors in degrees.

Once again, much of the code in this program is similar to code that I have explained before. I will explain only the method named actionPerformed , for which some of the code is new to this module. A complete listing ofthis program is provided in Listing 10 .

Beginning of the actionPerformed method in the program named DotProd2D02

This method is called to respond to a click on the OK button in Figure 2 .

Listing 6 . Beginning of the actionPerformed method in the program named DotProd2D02.
public void actionPerformed(ActionEvent e){//Erase the off-screen image and draw the axes. setCoordinateFrame(g2D);//Create two ColMatrix2D objects based on the user// input values. GM02.ColMatrix2D matrixA = new GM02.ColMatrix2D(Double.parseDouble(vectorAx.getText()), Double.parseDouble(vectorAy.getText()));GM02.ColMatrix2D matrixB = new GM02.ColMatrix2D(Double.parseDouble(vectorBx.getText()), Double.parseDouble(vectorBy.getText()));//Use the ColMatrix2D objects to create two Vector2D// objects. GM02.Vector2D vecA = new GM02.Vector2D(matrixA);GM02.Vector2D vecB = new GM02.Vector2D(matrixB);//Draw the two vectors with their tails at the origin. g2D.setColor(Color.BLACK);vecA.draw( g2D,new GM02.Point2D(new GM02.ColMatrix2D(0,0)));g2D.setColor(Color.MAGENTA); vecB.draw(g2D,new GM02.Point2D(new GM02.ColMatrix2D(0,0)));

Listing 6 gets the four user input values that define the two vectors and draws them in black and magenta on the GUI shown in Figure 3 . There is nothing new in the code in Listing 6 .

Compute the dot product and the angle between the two vectors

Listing 7 computes the dot product and the angle between the two vectors by first calling the dot method and then the angle method on the object referred to by vecA , passing the object referred to by vecB as a parameter.

Listing 7 . Compute the dot product and the angle between the two vectors.
//Compute the dot product of the two vectors. double dotProd = vecA.dot(vecB);//Output formatting code was deleted for brevity //Compute the angle between the two vectors.double angle = vecA.angle(vecB); //Output formatting code was deleted for brevitymyCanvas.repaint();//Copy off-screen image to canvas. }//end actionPerformed

In both cases, the code in Listing 7 formats the returned double values to make them appropriate for display in the bottom two text fields in Figure 3 . This code was deleted from Listing 7 for brevity.

Confirm that the results are correct

Because this is a 2D display, it is easy to make an eyeball comparisonbetween the drawing of the two vectors and the reported angle between the two vectors to confirm agreement. However, I recommend that you use thisprogram to define several vectors and then use your scientific calculator to confirm that the results shown are correct.

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