<< Chapter < Page Chapter >> Page >

Draw the two lines on the screen

Finally, Listing 7 calls the draw method of the Graphics2D class twice in succession to draw the two lines on the screen as shown in Figure 1 .

Listing 7 . Draw the two lines on the screen.
g2.draw(horizLine); g2.draw(vertLine);}//end overridden paint() }//end inner class MyCanvas}//end class GUI

Listing 7 also signals the end of the overridden paint method, the end of the inner class named MyCanvas , and the end of the top-level class named GUI .

But wait, there's something wrong here

Kjell describes his points and lines in a very general way that is suitable for use in mathematical operations later on. However, in the above program, Ifell into the trap of defining my points and lines using Java classes that are intended primarily for rendering graphics on the screen. That approach isprobably not conducive to mathematical operations. We need to step back and take another look at what we are doing here.

Points, points, and more points

There will be many occasions when you, as a game programmer, will need to define the coordinate values for a point (or a set of points) that you have no intention of displaying on the screen. Instead, you will use thosepoints for various mathematical operations to produce something else that may or may not bedisplayed on the screen.

The Alice ice skater

For example, Figure 2 , Figure 3 , and Figure 4 show three views in the construction of the Ice skater object in the Alice programming language .

Figure 2 First of three views in the construction of a 3D human figure.

Missing image.

Figure 3 Second of three views in the construction of a 3D human figure.

Missing image.

Figure 4 Third of three views in the construction of a 3D human figure.

Missing image.

Points as the vertices of triangles

The image in Figure 2 shows a graphical representation of a set of points. These points wereused to define the vertices of the set of polygons shown i n Figure 3 . The polygons, in turn, were used in the construction of the ice skater object shownin Figure 4 .

As you can see, neither the points in Figure 2 , nor the lines that comprise the sides of the polygons in i n Figure 3 appear in the final rendering of the object in Figure 4 .

However, both the points and the polygons were required to support the mathematical operations that ultimatelyresulted in the ice skater object.

(Only the image in Figure 4 is typically displayed on the computer screen. I had to do some extra work to cause the points and thelines to be displayed.)

Another look at points and lines

We'll take another look at points and lines, and will introduce column matrices and vectors in the next sample program.

What is a 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."

In addition, Kjell tells us that we can represent a vector with two real numbers in a 2D system and with three real numbers in a 3D system.

Use a column matrix to represent a vector

A column matrix provides a good way to store two real numbers in a computer program. Therefore, in addition to representing a point, a column matrix canalso be used to represent a vector.

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