Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mathematics for Game Programming and Computer Graphics

You're reading from  Mathematics for Game Programming and Computer Graphics

Product type Book
Published in Nov 2022
Publisher Packt
ISBN-13 9781801077330
Pages 444 pages
Edition 1st Edition
Languages
Author (1):
Penny de Byl Penny de Byl
Profile icon Penny de Byl

Table of Contents (26) Chapters

Preface 1. Part 1 – Essential Tools
2. Chapter 1: Hello Graphics Window: You’re On Your Way 3. Chapter 2: Let’s Start Drawing 4. Chapter 3: Line Plotting Pixel by Pixel 5. Chapter 4: Graphics and Game Engine Components 6. Chapter 5: Let’s Light It Up! 7. Chapter 6: Updating and Drawing the Graphics Environment 8. Chapter 7: Interactions with the Keyboard and Mouse for Dynamic Graphics Programs 9. Part 2 – Essential Trigonometry
10. Chapter 8: Reviewing Our Knowledge of Triangles 11. Chapter 9: Practicing Vector Essentials 12. Chapter 10: Getting Acquainted with Lines, Rays, and Normals 13. Chapter 11: Manipulating the Light and Texture of Triangles 14. Part 3 – Essential Transformations
15. Chapter 12: Mastering Affine Transformations 16. Chapter 13: Understanding the Importance of Matrices 17. Chapter 14: Working with Coordinate Spaces 18. Chapter 15: Navigating the View Space 19. Chapter 16: Rotating with Quaternions 20. Part 4 – Essential Rendering Techniques
21. Chapter 17: Vertex and Fragment Shading 22. Chapter 18: Customizing the Render Pipeline 23. Chapter 19: Rendering Visual Realism Like a Pro 24. Index 25. Other Books You May Enjoy

Reviewing Our Knowledge of Triangles

If there’s one area of mathematics that you want to focus on during your educational journey through graphics and games programming, make sure it is vector mathematics. I cannot stress enough how important having a thorough knowledge of this area is to your success in developing your skills in this domain. Trigonometry, which examines the relationships between side lengths and angles in triangles, underpins many of the vector functions. As such, we will be exploring the magic of triangles herein.

Initially, for me, vectors and triangles were taught as separate ideas. It wasn’t until I started using vectors in my game programming that it suddenly became clear how understanding triangles was such a big part of understanding vectors. Also, vectors are just an extension of the mathematics of triangles.

Triangles would have to be one of the most interesting and useful geometrical shapes. Mathematically, their properties are fundamental...

Technical requirements

The solution files containing the code for this chapter can be found on GitHub at https://github.com/PacktPublishing/Mathematics-for-Game-Programming-and-Computer-Graphics/tree/main/Chapter08 in the Chapter08 folder.

For the practical exercise in this chapter, you will require the teapot.obj model file, which can be downloaded from https://github.com/PacktPublishing/Mathematics-for-Game-Programming-and-Computer-Graphics/tree/main/Chapter08/resources.

Comparing similar triangles

Two triangles are similar when the only difference is the scale, as shown in Figure 8.1, where Triangle (b) is a scaled-down version of Triangle (a):

Figure 8.1: Similar triangles

Two triangles are considered similar if they contain the same set of three corner angles. For example, if the angles (in degrees) of Triangle (a) are 60, 45, and 75, then the angles of Triangle (b) must also be 60, 45, and 75. The total of all angles in a triangle equates to 180 degrees.

For similar triangles, the following applies:

  • All corresponding angles are equal
  • All corresponding sides have the same ratio
  • Corresponding sides have the same ratio between them

As triangles are only considered similar if the respective angles are the same between them, as previously discussed. Given Figure 8.1, A = α, B = β, and Γ = γ.

Because, with similar triangles, one is smaller than the other, the ratio of the corresponding...

Working with right-angled triangles

Since mathematics in grade school, we’ve learned about Pythagoras. He was the Greek mathematician who came up with the relationship between the lengths of the sides of a right-angled triangle. This relationship is so important to vector mathematics that it is worth learning it off by heart. It states that for a right-angled triangle, the square of the hypotenuse (the longest side) is equal to the sum of the squares of the other two sides. Such a triangle is illustrated in Figure 8.5, with c indicating the hypotenuse:

Figure 8.5: A right-angled triangle

Given the right-angled triangle in Figure 8.5, the relationship between the hypotenuse and other sides can be written like this:

Important note

The hypotenuse is the side opposite the right angle.

Let’s work through the calculation of the hypotenuse using the triangle in Figure 8.6:

Figure 8.6: A right-angled triangle (with the...

Calculating angles with sine, cosine, and tangent

The length of the hypotenuse and the other two sides can be determined using the triangle’s angles and the sine, cosine, and tangent operations. The sides of a right-angled triangle, apart from the hypotenuse, can be labeled as Opposite and Adjacent based on their positions in the triangle relative to the relevant angle, as shown in Figure 8.8:

Figure 8.8: The adjacent and opposite triangle sides for

Note that for these rules, either angle can be used. If the angle at the other end of the hypotenuse (other than what is shown in Figure 8.8) is used, then the labels for Opposite and Adjacent change positions.

The relationship between the sides and hypotenuse that represent the trigonometric function of sine is as follows:

The relationship for the trigonometric function of cosine is as follows:

The relationship for the trigonometric function of tangent is as follows:

Working with...

Summary

An understanding of triangles is essential for those who want to program in the graphics and games domains. They are the foundation of the data structures used to store meshes, as well as the basic drawing components of them. When it comes to mathematics in these realms, they are equally as important. Their trigonometric properties, as you will see in the next chapter, form the basis of vector mathematics.

In this chapter, you explored the fundamental properties of triangles and learned how to calculate the length of a triangle’s sides and angles using the relationships between similar triangles and the trigonometric rules found in right-angled triangles. Following the theory, we applied our knowledge of triangles to load a triangulated mesh into OpenGL and display it.

Now that you have explored the mathematics of triangles, we can start examining vectors.

Answers

Exercise A:

These two triangles are similar. You only need two corresponding angles and two corresponding sides to determine this fact. Both triangles have angles of 60 and 75. This is enough to establish that the triangles are similar because if you take 60 and 75 away from 180 (the total of the angles in a triangle), then the remaining angle of K for Triangle (h) will be 45 and the corresponding angle in Triangle (g) will also be 45.

To find the length of J, you need to establish the ratio of the other corresponding sides. As the triangles are similar, you will know that the known two sides will have the same ratio – that is, 3/2 = 2.8/1.86 = 1.5.

Using this ratio, we can calculate J to be 4.8/1.5 = 3.2.

Exercise B:

This is the value of x in Triangle (j):

This is the value of x in Triangle (k):

Exercise C:

(X) To find θ, we must use the cosine rule, which, when written as a Google search, becomes arccos...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Mathematics for Game Programming and Computer Graphics
Published in: Nov 2022 Publisher: Packt ISBN-13: 9781801077330
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}