Maths and Geometry Primer

Learn the basics of maths and geometry, so we can represent concepts like positions and distances between objects in our worlds.
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

3D art showing a teacher in a classroom
Ryan McCombe
Ryan McCombe
Posted

We want to start simulating basic world which we can imagine our objects existing in. To do this, we will need to represent concepts like positioning, distance and movement.

This and the following chapter underpins the minimal topics in maths that we need in order to start to model these relationships.

We'll cover some fundamental arithmetic and then move onto basic geometry. Finally, we'll see how we can apply what we've learnt to determine how far away two objects are in a 2D or 3D world.

Exponentiation

Exponentiation is the process of "raising" a number to the power of another number. We can think of it as multiplying a number by itself, multiple times.

The number we start with is called the "base", and the number of times it is multiplied by itself is called the "power", or the "exponent". For example:

  • 22 to the power of 22 is equivalent to 2×22 \times 2, which is equal to 44
  • 22 to the power of 33 is equivalent to 2×2×22 \times 2 \times 2, which is equal to 88
  • 33 to the power of 22 is equivalent to 3×33 \times 3, which is equal to 99

We generally represent this operation with a superscript notation, for example:

  • 22 to the power of 22 is written as 222^2
  • 22 to the power of 33 is written as 232^3
  • 33 to the power of 22 is written as 323^2

The most common form of exponentiation we will see is raising a number to the power of 22, for example, 525^2.

An operation like this, where the exponent is 22, is commonly called squaring. The result of such an operation is called the square of the base number.

For example, the square of 55 is 2525, because 52=255^2 = 25.

Order Of Operations

In the order of operations, exponentiation happens before multiplication, but after brackets / parentheses. For example:

  • 2×22=2×4=82 \times 2^2 = 2 \times 4 = 8
  • (2×2)2=42=16(2 \times 2)^2 = 4^2 = 16

Square Root

Finding the square root of a number is, in effect, reversing the process of squaring. For example, the square root of 25 is the number that, if squared, would result in 25.

In other words, the square root of 2525 is the value of xx such that x2=25x^2 = 25. We saw, from the previous section, that an answer is 55, because 52=255^2 = 25. Therefore, the square root of 2525 is 55

Square roots are often denoted using the radical sign, which looks like this: 25\sqrt{25}

Expressions of any length can appear under a radical sign. That expression should be resolved before calculating the square root. For example: 50+25×2=100=10\sqrt{50 + 25 \times 2} = \sqrt{100} = 10

Multiple Roots

You may have noted that our earlier equation, x2=25x^2 = 25, has multiple possible solutions:

  • 52=255^2 = 25
  • 52=25-5^2 = 25

This means that both 55 and 5-5 are solutions (or roots) to the equation x2=25x^2 = 25.

Most commonly, we are only looking for the positive root, sometimes also called the principal root. When using the radical notation, such as 25\sqrt{25}, the only solution is the positive root.

  • 25=5\sqrt{25} = 5
  • 255\sqrt{25} \neq -5

Spaces

We can think of virtual worlds as spaces. In maths and physics, a space contains a collection of objects that obey pre-defined structure and rules. Typically, the structure and rules we want to implement mirror those we are most familiar with in the real world.

That means we will be interested in things like the distance between objects, and rules that govern how objects can move around the space.

The most basic object that exists within spaces are points. We can model more complex objects by creating a collection of such points - for example, a triangle could be defined by 3 points - one for each of its corners. A full 3D monster, created by an artist, could have thousands of points to define it's shape.

The most important property we care about when it comes to these points is what their current position is within the space. We can implement the concept of position by imagining our space as a grid.

For now, we will work with a two-dimensional space:

A blank grid

Coordinate Systems

The system we most commonly use to define positions in a space is a coordinate system. The most useful system is the Cartesian coordinate system.

We can set up this system by placing an axis along each dimension. Conventionally, we call the horizontal axis the xx axis, and the vertical axis the yy axis.

A blank grid including coordinate axes

With the axes set up, we can now define each point by a collection of numbers - one for each dimension the space has. In a two-dimensional space, each point will have two values - one for its position along the xx axis, and one for it's positiong along yy .

A 2D grid with coordinate axes, and two points shown

Components and Vectors

In the above example, Point AA's xx component is 33, and it's yy component is 44. We can write this point as A=(3,4)A = (3, 4).

In our lessons on classes and structs, we saw how we might refer to the collection of all the components required to represent a point as being a vector.

In C++, we modelled the idea of a vector using a struct, which then let us access each individual component using the . operator.

For example, A.x is how we would access to the x component of a vector called A. Outside of code, it's common to use subscript notation, such as AxA_x.

We could also describe the point as xAx_A if we preferred. Notation in maths and physics is less strict than code - we can choose whatever format we think best communicates our thoughts. The important thing is just to be consistant.

The Origin

In a cartesian coordinate system, the point that all the axes intersect is referred to the origin. The origin has a value of 0 in every dimension, and can generally be considered the centre of the space.

In the previous example, point BB is at the origin of our space, as B=(0,0)B = (0, 0). The origin is also sometimes denoted by the letter OO.

Angles

When two lines intersect, they form angles. On diagrams, angles of interest are marked using a circular segment between two intersecting lines:

A diagram showing a small angle

Angles are commonly measured in degrees, which is denoted by number followed by the degree sign: °\degree

The wider an angle, the more degrees it has.

A diagram showing two angles

A complete turn is 360°360\degree and a quarter turn is 90°90\degree

A 90°90\degree angle is often referred to as a right angle and, on diagrams, is commonly denoted by a square icon:

A diagram showing a right angle

Right-Angled Triangles

A right-angled triangle is a triangle where two of the sides are perpendicular to each other, forming a right angle.

Right-angled triangles tend to be illustrated with a square in the right angle.

The side opposite this angle is referred to as the hypotenuse, and will be the longest side of the triangle.

Right angle triangles and their properties are useful to know, as many problems we encounter can be modeled as right-angled triangles.

For example, we can imagine any two points in our space as forming the hypotenuse of a right angle triangle. Below, we illustrate this with point AA and the origin.

A diagram showing a right angle triangle on a grid with its vertices labelled

By understanding this situation as involving a right angle triangle, we can use the properties of right angle triangles to work out the distance between our two points.

This has many uses - for example, it is the basis of determining whether or not a monster is in range of our attacks.

We can use the Pythagorean Theorem to work out this distance.

Pythagorean Theorem

The pythagorean theorem allows us to calculate the length of a right-angled triangle's hypotenuse if we know the length of its two other sides.

For example, lets imagine we know the lengths of the two smaller sides, aa and bb, and wanted to work out the length of the longest side, the hypotenuse hh

An example of a right angled triangle

The pythagorean theorem stats that:

h=a2+b2h = \sqrt{a^2 + b^2}

Let's apply it to find out how far away point AA is from the origin:

A diagram showing a right angle triangle on a grid

If we wanted to know how far away point AA is from the origin, that is equivalent to finding the length of the highlighted hypotenuse.

So, lets apply the pythagorean theorem:

h=32+42=9+16=25=5\begin{align} h &= \sqrt{3^2 + 4^2} \\ &= \sqrt{9 + 16} \\ &= \sqrt{25} \\ &= 5 \end{align}

So, point AA is 55 units away from the origin.

The pythagorean theorem is particularly useful when dealing with spaces, because we always know the length of the smaller sides of our right angle triangle.

They are the values of our co-ordinates, so we can just apply the same pattern with different inputs.

Lets imagine we have the point (6,8)(6,8). How far away is it from the origin?

h=62+82=36+64=100=10\begin{align} h &= \sqrt{6^2 + 8^2} \\ &= \sqrt{36 + 64} \\ &= \sqrt{100} \\ &= 10 \end{align}

Any situation where we're applying the same process, only with different numbers, might get our programmer senses tingling. This sounds a lot like a function, and we'll be creating it in the next lesson.

Distance Between Any Two Points

It is not especially useful to know how far away an object is from the origin. More often, we will want to know how far away our object is from some other object.

The technique using the pythagorean theorem applies to this situation, too. We can create a right angled triangle from any two points:

A diagram showing a right angle triangle on a grid, offset from the origin

In this example, the side lengths of our right angle triangle have a length of 33 and 77, so we can calculate how far apart point AA and BB are.

h=52+32=25+9=345.8\begin{align} h &= \sqrt{5^2 + 3^2} \\ &= \sqrt{25 + 9} \\ &= \sqrt{34} \\ &\approx 5.8 \end{align}

Whilst the selection of the 55 and 33 may have seemed intuitive by looking at a our diagram, we generally won't have a visual representation to work from.

We also want to write code that will work with any input points, therefore, it is worth detailing how we would come to these values in a more general case.

For example, AA and BB will just be points passed to our function. We don't know their position ahead of time - we have to work it out. So how would we get the values that were, in this case, 55 and 33?

  • We took the xx coordinate of point BB and subtracted the xx coordinate of point AA. That gave us 727 - 2
  • We took the yy coordinate of point BB and subtracted the yy coordinate of point AA. That gave us 414 - 1

In other words, we did xBxAx_\text{B} - x_\text{A} and yByAy_\text{B} - y_\text{A} to get the two sides of our right angled triangle.

Distance Formula

This allows us to expand the pythagorean theorem to a new formula, to calculate the distance between any two points. Lets call our points AA and BB and, for now, we'll assume we're working in a two-dimensional space:

(xAxB)2+(yAyB)2\sqrt{(x_\text{A} - x_\text{B})^2 + (y_\text{A} - y_\text{B})^2}

This formula is sometimes referred to as the distance formula.

How do I choose which point is AA and which is BB?

Something to note about this formula is that it does not matter which point you choose to be AA and which is BB. The only effect this choice will have will be to determine whether the result of xAxBx_\text{A} - x_\text{B} is negative or positive.

However, that result is being immediately squared, so the sign will be discarded anyway. Squaring a number will give the same result whether that number is negative or positive.

This is because multiplying two negative numbers together will always result in a positive number. For example, 525^2 and 52-5^2 are both 2525.

Therefore, (xAxB)2(x_\text{A} - x_\text{B})^2 will always be positive, so it doesn't matter which point is chosen to be AA and which is chosen to be BB.

Going 3D

The examples above used two-dimensions in their examples and illustrations, but the techniques apply to three dimensional worlds too.

In cartesian co-ordinate systems, the 3rd dimension is generally identified by the letter zz, to give co-ordinates (x,y,z)(x, y, z). The origin of a three-dimensional cartesian space is (0,0,0)(0, 0, 0)

The pythagorean theorem expands to three dimensions. To calculate how far a three-dimensional point (x,y,z)(x, y, z) is from the origin, we can use this:

x2+y2+z2\sqrt{x^2 + y^2 + z^2}

Similarly, the distance formula also expands to three dimensions. To calculate the distance between point AA with coordinates (xA,yA,zA)(x_\text{A}, y_\text{A}, z_\text{A}) and point BB with coordinates (xB,yB,zB)(x_\text{B}, y_\text{B}, z_\text{B}) we can use this:

(xAxB)2+(yAyB)2+(zAzB)2\sqrt{(x_\text{A} - x_\text{B})^2 + (y_\text{A} - y_\text{B})^2 + (z_\text{A} - z_\text{B})^2}

Was this lesson useful?

Ryan McCombe
Ryan McCombe
Posted
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Odds and Ends
3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access!

This course includes:

  • 66 Lessons
  • Over 200 Quiz Questions
  • Capstone Project
  • Regularly Updated
  • Help and FAQ
Next Lesson

Standard Library Maths Helpers

Learn how to implement some more advanced math functions, with some help from the C++ standard library.
3D art showing a character studying maths
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved