# Using Trigonometry to Animate Bounces, Draw Clocks, and Point Cannons at a Target

Python programs in this tutorial. Requires Python and Pygame to be installed.)

Trigonometry is the branch of mathematics that studies triangles and the relationships between their sides and the angles between these sides. The sine, cosine, and tangent trigonometry functions are implemented as programming functions in most languages and given the names sin(), cos(), and tan(). In Python, these functions exist in the `math` module. These functions are very handy in games and graphical programs because of the smooth wave-like pattern their return values produce.

This tutorial shows how you can get a variety of neat animation behavior using these functions in your programs. (The animated gifs you see above were taken from the programs in this tutorial.) The code examples in this tutorial should work with both Python 2 and Python 3.

### Trig Function Basics

You don’t need to know how these functions work. Let’s just treat these functions as black boxes: they take one number parameter in, and return a float value out. If you already know the mathematics of sine and cosine, then you can just skim this section.

In the interactive shell, let’s see what `math.sin()` returns for some values:

>>> import math

>>> math.sin(1)

0.8414709848078965

>>> math.sin(2)

0.90929742682568171

>>> math.sin(3)

0.14112000805986721

>>> math.sin(4)

-0.7568024953079282

>>> math.sin(5)

-0.95892427466313845

It looks like `math.sin()` just spits out some random-looking float values. (Actually, it’s the length of the opposite side divided by the hypotenuse of a right triangle with the given angle. But you don’t need to know or understand this.) But if we graph the return values of the input arguments `1` through `10` on a graph, we can see the pattern:

If you figure out the sine values for some more numbers (for example, `1.5` and `1.5` and so on) and then connect the dots with lines, you can see this wave pattern more easily:

In fact, if you kept adding more and more data points to this graph, you would see that the complete sine wave looks like this:

The significant input/output pairs for sine are:

Math Function

Python Code

Return Value

sine of 0

`math.sin(0)`

0

sine of π / 2

`math.sin(math.pi / 2)`

1

sine of π

`math.sin(math.pi)`

0

sine of 3π / 2

`math.sin(3 * math.pi / 2)`

-1

sine of 2π

`math.sin(2 * math.pi)`

0

Page 1 of 5 | Next page