# Are you sorry you slept through Trigonometry yet? You will be.

In all my math classes, from Algebra to Geometry right on up to Calculus, Trigonometry was always the afterthought. It’s a shame because as soon as i hear someone say “Math never helped me in real life!” I immediately think that’s only because they don’t remember their Trigonometry. I was lucky enough to have a tutor who was really passionate about getting me to understand Trig, especially the Unit Circle, and even though I don’t remember everything off hand I will always remember just how valuable this often maligned area of mathematics can be.

Trigonometry is a fancy name for something really simple: Dividing up circles into angles. The awesome things about a circle is that: 1) it’s continual across its whole surface, and 2) it governs the full range of possible angles. We put these two qualities together and get the basic Trigonometric functions. So the basic unit of understanding for Trigonometry is the Unit circle, and that’s where we’ll start.

Here you have the Unit Circle. It looks really complicated but actually all you need to know remember is that it’s just a circle divided into your basic angles. Here’s how we do that: We know the circumference of a circle is 2π, so we divide the circle into imaginary sections that are called Radians, and there are 2π of them in a circle. Assuming we know something about angles, you can tell that we have a few easy radian values that we can turn into angles just by halving the total amount of radians:

• All the way around the circle — think of this as 360 degrees — 2π radians
• Half way around the circle — this is 180 degrees — π radians
• Half that again, a quarter of the circle — this is 90 degrees — π/2 radians
• Half that last quarter — 45 degrees — is π/4 radians

So halving is great, but how do we get some other important angles, or how do we get any angle we want? Well, what I like to do is go in relation to 90 degrees. If I want 30 degrees, I know that’s 1/3 of 90 degrees. Oh no! now we have to divide fractions! I used to hate this, but nowadays I know it’s no big deal. Just like a half of π/2 is π/4, 1/3 of π/2 (that’s 90 degrees, remember) is π/6. So now we can go from here to get any angle we want, so long as it’s an even factor of 90:

• Remember! 90 degrees is π/2 radians.
• 30 degrees: π/6 radians (30 goes into 90 3 times, as described 1/3 of π/2 is π/6)
• 10 degrees: π/18 radians (because 10 goes into 30 3 times)

Whoa! Look at that last guy! How’d we get there? Well remember that 180 degrees is π radians… Well we know we can divide any number by itself to get 1 — so that means if we divide π radians by 180, 1 degree must be π/180 radians!
Now we can translate any any angle into radians! Just take the number of degrees you want and multiply that by the fraction π/180 ! If you’re paralyzed in fear of math, you don’t even need to reduce!
(congratulations on getting this far by the way : )

“Hey, wait a minute!” says my imaginary math-impaired reader, “Isn’t this a Swift blog? How does this apply to Swift? Well, funny you should ask! The CGAffineTransform object takes its rotation angle in radians. Since radians go counterclockwise, if you want to rotate your object 29 degrees to the right, you can just code:
CGAffineTransform(rotationAngle: -29 * ( CGFloat.pi/180) )

So, I could venture a guess from the fact that CGAffineTransform works best with pi in its rotation angle parameter, that it must use a sine or cosine function to do its magic. You can use the sine and cosine functions to turn your angle measurement in radians into the x and y value of a coordinate on the unit circle. The unit circle is a circle of radius 1, so if you want to take a point on that circle and rotate it by an angle of some kind, you translate the angle into radians, then use sine and cosine to get the coordinate. When you put a radian measure into a sine function, you get its y coordinate on the unit circle. Likewise when you put one into the cosine function, you get its x coordinate.

These functions, sine and cosine, are really useful because since they oscillate between -1 and 1 in a very smooth fashion. And when you multiply some number by pi inside a sine function, that oscillation happens every time that value goes up by one. This lets us do lots of really cool things.

For instance you could hook up a cosine function to a UIView’s .alpha property to have it fade smoothly in and out as someone scrolls. Since we know that Alpha has to be between 0 and 1, we know we have to make cosine produce its lowest value at 0 so we add +1. Now cosine is going to oscillate between 0 and 2 though, so we are going to divide that whole thing by 1/2 so that it can stay within 0 and 1. So to have our alpha variate with respect to some value, we can code:
someObject.alpha = (1/2)* (cos(pi*someValue) +1)
From here you can experiment with different ways to make the fade slower or faster. Google now graphs when you search “graph” and then a mathematical function, so that is really useful for playing around with this kind of thing right in your web browser.

If you’re having trouble visualizing the relationship between sine, cosine, and the unit circle, here’s a neat little .gif from wikipedia to help out:

So that’s all we’re going to cover for today, clas — I mean, dear mathematically challenged readers! I do hope this knowledge comes in handy when rotating and oscillating things in Swift and beyond!