Sine and cosine

Anode and cathode, procaryote and eucaryote, epimorphism and monomorphism. For every pair, I think I know what the words mean from school but I can't always tell one from another. I can always tell sine from cosine but I didn't learn them at school. My father taught me.

He was not an educator but an engineer. He didn't tell me where these words come from, he didn't tell me about Greeks or Sumerians. He didn't even tell me about that circle thingy with a triangle. Nothing that math teachers somehow deem important. He only told me what do these functions look like and how to make them into other functions. I played with them on a computer for a while and that was it. Every piece of trigonometry I learned from then started to make sense.

Sine function

This is sine.

It's a continuous function that runs through all the x-axis. On the y-axis, it is constrained between -1 and 1.

Its most interesting values are:

α in degrees 0° 30° 45° 60° 90° α in radians 0 Π / 6 Π / 4 Π / 3 Π / 2 sin(α) √0 / 2 √1 / 2 √2 / 2 √3 / 2 √4 / 2

It is smooth. It grows and declines but the magnitude of these changes is continuos.

It is periodic meaning that it consists of an infinite number of equal pieces 2Π-long.

It is antisymmetric meaning that it's x-negative half is exactly the opposite of the x-positive one: f(-x) = -f(x).

In 0 it grows exactly like y = x. For small x, we can even use f(x) = x as an approximation of sin(x).

It's all pretty easy to remember since it also looks like S.

Toggle the mnemonic

Cosine function

This is cosine.

It is the sine's older cousin. It looks exactly like the sine but it runs Π/2 ahead of it.

It has the same interesting values but in reverse order:

α in degrees 0° 30° 45° 60° 90° α in radians 0 Π / 6 Π / 4 Π / 3 Π / 2 sin(α) √4 / 2 √3 / 2 √2 / 2 √1 / 2 √0 / 2

Unlike the sine, it's symmetric. Its x-negative half is the mirrored copy of x-positive half: f(-x) = +f(x).

Around 0, it behaves like y = 1.

It's also easy to remember since it looks like C.

Toggle the mnemonic

The moving illustrations you see are all made with sines and cosines. And now I'll show you how.

Rotation

Rotation is a spatial transformation. On a plane, this is a function that turns a pair of coordinates into another pair of coordinates. (x, y) → (x r , y r ). Of course, it can be written down as a pair of functions used simultaneously.

The formula for rotation on a in radians is this:

x r = -x cos(a) + y sin(a)

y r = x sin(a) + y cos(a)

On this plot, I wanted the arrow to run clockwise so I negated the a. But you can do the same by altering your formula. And yes, it's your formula now, the code below is changeable, you can change things there and the plot will react.

const screen_center = [320, 320]; const a = - t_ms / 1000; // time to angle proportion // this is how you rotate: x, y, a -> x_rotated, y_rotated function x_rotated(x, y) { return - x * cos(a) + y * sin(a); } function y_rotated(x, y) { return + x * sin(a) + y * cos(a); } // the illustration const arrow = [[0, 0], [200, 0], [180, -7], [200, 0], [180, 7]]; context.moveTo(screen_center[0], screen_center[1]); for(var i = 1; i < arrow.length; ++i) context.lineTo( screen_center[0] + x_rotated(arrow[i][0], arrow[i][1]), screen_center[1] + y_rotated(arrow[i][0], arrow[i][1]));

“Natural” movement

If something starts moving gradually growing speed from the full stop and then stops completely by gradually losing speed, we consider its movement natural.

On the plot, the vertical line speeds up from 0 and stops smoothly at the end. That's because its movement is dictated by f(x) — the function that starts flat and ends flat, and we use the sine for that, and the sine itself is smooth.

const w = 640; const h = 640; // the function f: x -> y; x ∈ [0, 1], y ∈ [0, 1] function f(x) { return (sin((x - 0.5)*PI) + 1) / 2; } // the plot for(var j = 0; j < w ; ++j) { const i = h - h * f(j / w); if(j == 0) context.moveTo(j, i); else context.lineTo(j, i); } // the moving line const t = (t_ms / 5000) % 1; context.moveTo(f(t) * w, 0); context.lineTo(f(t) * w, h);

Curves

A parametric curve is a function of a parameter resulting in a pair of coordinates: a → (x, y).

Of course, it's the same as a pair of separate functions for each coordinate.

const w = 640; const h = 640; const size = 280; // parametric curve is: a -> (x, y) function x(a) { return w / 2 + sin(a) * size; } function y(a) { return h / 2 - PI / 2 * sin(a)*cos(a) * size; } // this draws the function in a range const range = [-0.75 * PI, 0.75 * PI]; for(var a = range[0]; a <= range[1]; a += 0.01) { if(a == range[0]) context.moveTo(x(a), y(a)); else context.lineTo(x(a), y(a)); }

TL&DR

Sine is like S and cosine is like C.

You can make things like O or 8 by using them both.