<< Chapter < Page Chapter >> Page >

The actionPerformed method in the program named DotProd2D01

The beginning of the actionPerformed method is shown in Listing 4 . This method is called to respond to a click on the OK button shown in Figure 14 .

Listing 4 . The actionPerformed method in the program named DotProd2D01.
public void actionPerformed(ActionEvent e){ //Create two ColMatrix2D objects.GM02.ColMatrix2D matrixA = new GM02.ColMatrix2D( Double.parseDouble(colMatA0.getText()),Double.parseDouble(colMatA1.getText()));GM02.ColMatrix2D matrixB = new GM02.ColMatrix2D( Double.parseDouble(colMatB0.getText()),Double.parseDouble(colMatB1.getText()));//Compute the dot product. double dotProd = matrixA.dot(matrixB);

Listing 4 begins by instantiating a pair of GM02.ColMatrix2D objects using data provided by the user in the top four fields shown in Figure 14 .

Then Listing 4 calls the dot method on the object referred to by matrixA , passing the object referred to by matrixB as a parameter. The dot method computes the dot product of the two column matrix objects, returning the result as type double .

Format the dot product value for display in the GUI

In some cases, the format of the returned value is not very suitable for display in the lower-left field in Figure 14 . For example, if the value is very small, it is returned in an exponential notation requiring a large numberof digits for display. Similarly, sometimes the dot-product value is returned in a format something like 0.33333333 requiring a large number ofdigits to display.

Listing 5 formats the dot product value to make it suitable for display in the text field in Figure 14 . (There may be an easier way to do this, but I didn't want to take the time to figure out what it is.)

Listing 5 . Format the dot product value for display in the GUI.
//Eliminate exponential notation in the display. if(Math.abs(dotProd)<0.001){ dotProd = 0.0;}//end if//Convert to four decimal digits and display. dotProd =((int)(10000*dotProd))/10000.0;dotProduct.setText("" + dotProd); }//end actionPerformed

Eliminate exponential format and format to four decimal digits

Listing 5 begins by simply setting small values that are less than 0.001 to 0.0 to eliminate the exponential format for very small, non-zero values.

Then Listing 5 executes some code that formats the dot product value to four decimal digits. I will leave it as an exercise for the student to decipherhow this code does what it does.

I recommend that you try it

I recommend that you plug a few values into the input fields, click the OK button, and use your calculator to convince yourself that the program properlyimplements the 2D dot product equation shown earlier .

That concludes the discussion of the program named DotProd2D01 .

The program named DotProd2D02

To understand this program, you need to understand the material in the Kjell tutorial through Chapter 9, The Angle Between Two Vectors .

This program allows the user to experiment with the dot product and the angle between a pair of GM02.Vector2D objects.

A screen shot of the output from this program is shown in Figure 3 . The GUI shown in Figure 3 is provided to allow the user to enter four double values that define each of two GM02.Vector2D objects. The GUI also provides an OK button as well as two text fields used for display of computed results.

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