R Lesson 32: Basic Trigonometry, R Style

Hello everybody,

So far this year, we’ve explored the basics on making our own Python game with the pygame module-pretty cool right! After all, it was the first time this blog delved into game development.

However, building a good game takes time; this includes the BINGO game we’ve been developing. With that said, I’ll need to spend a little more time fine-tuning the BINGO game and planning out the game development series going forward. I’d hate to go too long without keeping fresh content on this blog, so with that in mind, in today’s post, we’ll explore something a little different-R trigonometry (you may recall I did a number of R mathematics posts last year)

Last year, we explored some basic R calculus-this year, we’ll explore basic R trigonometry. For those who don’t know what I’m talking about, trigonometry is a branch of mathematics that deals with the study of triangles and their angles.

Trigonometry basics

Before we get into the fun of exploring trig with R, let’s explore some basic trigonometry terms using this handy-dandy illustration:

Here we have a simple right triangle with a 90 degree angle, a 52 degree angle, and a 38 degree angle along with two sides of lengths 10 and 7 and a hyptoensue of length 12.2 (remember the Pythagorean theorem for right triangles-to find the length of the hypotenuse, a squared + b squared = c squared).

If you’ve ever taken at least precalculus with basic trigonometry, you’ll certainly recognize the mnemonic on the upper-right hand corner of the screen-SOHCAHTOA. If you’re not familiar with this mnemonic, here’s what it means:

  • SOH: To find the sine of an angle in the triangle, take the ratio of the length of the side opposite the angle to the length of the hypotenuse
  • CAH: To find the cosine of an angle in the triangle, take the ratio of the length of the side adjacent to the hypotenuse to the length of the hypotenuse
  • TOA: To find the tangent of an angle in the triangle, take the ratio of the length of the side opposite to the hypotenuse to the length of the side adjacent to the hypotenuse

Trigonometry, R style

Now that we’ve discussed the basics of trigonometry, let’s discuss trigonometry, R style. Here are some examples of the three basic trigonometric functions executed in R:

> cos(20)
[1] 0.4080821
> tan(20)
[1] 2.237161
> sin(20)
[1] 0.9129453

In this example, I used R’s built-in cos(), tan() and sin() functions to calculate the cosine, tangent, and sine of a given angle, respectively. Since these functions are built-in to R, there’s no need to install any extra packages to utilize these functions

You may be wondering if these functions can calculate the sine/cosine/tangent of an angle given specific side lengths (as shown in the illustration above). The answer to that is no, but then again, you can easily calculate these trigonometric ratios using simple division. For instance, in the illustration above, the sine of the 52 degree angle in the triangle is ~0.82 (rounded to two decimal places) because the opposite/hypotenuse length ratio is 10/12.2.

So, how does R calculate trigonometric ratios of certain angles? They use a little something called radians, which I’ll explain more right here.

Radians

What are radians, exactly? Well, when measuring angles in shapes, there are two metrics we use-degrees (which I’m sure you’re familiar with) and radians.

Let’s take this illustration of a circle:

As you see, the length of this circle’s radius is 8-the arc of the circle that is formed also has a length of 8. Therefore, the angle that is formed at the circle’s center has a length of 1 radian.

Now, how do you convert radians to degrees. Easy-1 degree is equal to pi/180 radians.

Why are radians expressed as a ratio of pi? This is because, for one, the circumference of a circle is 2pi times the circle’s radius. For two, the length of a full circle is 360 degrees-or 2pi radians; similarly, the length of a half-circle is 180 degrees-or pi radians.

Now, let’s analyze how radians work in the context of our R examples (all of which used 20 degree angles).

I mentioned earlier that 1 degree equals pi/180 radians, so 20 degrees would be 20*(pi/180) radians, which when converted to simplest form, equals pi/9 radians, which is then used to calculate various trigonometric ratios for any given angle.

The degrees-to-radians formula is so versatile that in R, it can be used on any integer, positive or negative. Check out some of these examples:

> cos(-3)
[1] -0.9899925
> sin(355)
[1] -3.014435e-05
> tan(15000)
[1] -1.98891
> cos(412)
[1] -0.8998537
> sin(-1333)
[1] -0.8218865
> tan(10191)
[1] -0.338695

Yes, I can use R’s basic trigonometric functions on a wide variety of integers and obtain valid results from each integer (although let’s be real, where will you ever find a 15000 degree angle)?

Thanks for reading,

Michael