O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Functions and Equations
The concept of functions is fundamental to mathematics. A function
expresses the relationship between two (or more) terms called variables,
and is typically written in the form of an equation (an algebraic expression
set equal to another algebraic expression). Variables are named as such
because, as the name implies, their values may vary. Variables are usually
expressed with letters of the alphabet. The two most common variables you
will see used in mathematical equations are x and y (although any letter or
symbol is just as valid).
If each value of x can be associated with one value of y, then y is a func
-
tion of x. y is said to be the dependent variable since its value depends on
the value of x. Here are a couple of examples:
(1.1)
(1.2)
In the second example, the m and the c represent constants (sometimes
called coefficients) — values that never change no matter what the value
of x is. They are effectively similar to the 2 in equation (1.1). Therefore, if
a = 2, equation (1.1) can be written as follows:
(1.3)
Given any value of x, the corresponding y value can be calculated by put-
ting the x value into the function. Given x = 5 and x = 7 and the function
y =2x, the y values are:
(1.4)
This type of function, where y is only dependent on one other variable, is
called a single-variable function. Single-variable functions may be visual
-
ized by plotting them onto the xy Cartesian plane. To plot a function, all
you have to do is move along the x-axis and for each x value use the func
-
tion to calculate the y value. Of course, it’s impossible to plot the graph for
every value of x — that would take forever (literally) — so you must select
a range of values.
The left-hand side of Figure 1.3 shows how function y =2x looks when
plotted on the xy plane, using the range of x values between –5.0 and 5.0.
A Math and Physics Primer | 3
Mathematics
2yx=
ymxc=+
yax=
2(5) 10
2(7) 14
y
y
==
==
To plot the function y = mx + c to a graph, you must first have some values
for the constants m and c. Let’s say m = 2 and c = 3, giving the function
y =2x + 3. The right-hand side of Figure 1.3 shows the resulting graph.
The graphs look very similar, don’t they? That’s because y = mx + c is
the function that defines all straight lines in 2D space. The constant m
defines the line’s gradient, or how steep the slope of the line is, and the
constant c dictates where the line intersects the y-axis. The function y =2x,
shown on the left in the figure, is equivalent to the function y = mx + c,
when m = 2 and c = 0. The plot on the right is almost identical but because
its c value is 3, the point where it intersects the y-axis is shifted up by three
units.
Sometimes you will see a function such as y=mx+cwritten like this:
(1.5)
The notation f(x) is stating that the dependent variable — in this example,
the y — depends on the variable x in the expression given on the right-hand
side, mx+c. Often, you will see symbols other than an f to represent the
function, so don’t become confused if you come across something like the
following.
(1.6)
The g(x) represents exactly the same thing as if the equation was written
as:
(1.7)
4 | Chapter 1
Mathematics
Figure 1.3. Functions plotted in Cartesian space
()fx mx c=+
2
()gx x bx=+
2
()fx x bx=+

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required