Constants

When writing code involving math you'll often have to make use of numbers or values of other types that are unchanging. These are monolithic truths, universal properties, or definitive values that have never and will never change. These are "constants".

Keep in mind that even though these first variables are labeled "Radians to degrees" and back, this isn't the exact way of converting between the two types of rotation. It's more of a short hand, which is acceptable in most situations because unless you're simulating quantum mechanics you'll never need the incredibley small or large numbers produced by an algorithm like this:



// Constants just for ourselves

var MY_NAME = "Bob"

// Mathematical constants

var PI = 3.14159265359;
var GOLDEN = 1.6180339;
var APERYS = 1.2020569;
var SQUARE_OF_5 = 2.23606797749979;

// Conversion constants that are regularly used in geometry.

var RADIANS_TO_DEGREES = 180 / Math.PI;
var DEGREES_TO_RADIANS = Math.PI / 180.0;

// Or anything you need to NEVER change.

Why are constants written in all caps? Well the most obvious comparison is the above comment that constants NEVER change. See the importance?

Essentially this is a not-so-subtle reminder that the value of the referenced variable will not be changing. In JavaScript, since it is so easy to change variables at any time, it can be helpful to remind the author to NOT attempt to change particular values. It can also help identify, at a glance, important mathematic archtypes such as "Pi" that are used regularly in geometry calculations.

This is not a universal convention. In fact in some languages, especially compiled ones, it is advise that you NOT write constants in all caps because all caps is reserved for preprocessor and compiling options. But those would be languages such as C++ or Java (different than JavaScript).

Since JavaScript is not a compiled language, but a script, we have no such options to establish for a compiler.

Types of Rotation

There are multiple types of rotation that can be used in mathematics. You can store rotation as a classical "degree", as a "radian", as a "quaternion", or even in a matrix value. They each have their good and bad qualities.

I'm going to give a concise benefit and loss for each but highly suggest reading explanations by more knowledgeable sources than this one.

Degrees / Euler Angles

"Euler angles are three angles introduced by Leonhard Euler to describe the orientation of a rigid body with respect to a fixed coordinate system."

Any orientation can be achieved by rotating an object around 3 axis lines. These are akin to the "pitch, roll, and yaw" of a plane but in computer science we typically just say "x, y, and z". Which direction is which will change depending on your chosen system.

The main benefit of this system is that it is easy to understand, especially when using a graphical user interface to modify the orientation of a 3d object, and that it is easy to visualize modification of.

The reason they are easy to understand is that, in most programming math libraries, they are defined use degrees that fall between 0 and 360. This is an easy amount for most people to divide in their head.

Want half a circle? 180 degrees.

Want a quarter circle? 90 degrees.

Want an eighth of a circle? 45 degrees.

Each of these measurements allow you to quickly turn an object to face the exact opposite direction, turn it to face to the exact left or right, or turn it to a direction exactly diagonal from where it was.

The weakness of such a system is mostly mechanical. If you are trying to find distances based on rotation, using COS and SIN for instance, you simply can't do it without converting to radians first. Many operations of angular math are dependent on the inclusion of "Pi" as a value.

Furthermore, rotations tracked on three axis are susceptible to "gimbal lock". This is yet another concept that is already explained better on Wikipedia.

Radians

Why radians? Well they have an actual reason for existing for one. A radian is "the ratio between the length of an arc and its radius". It's actually based on other measureable properties of an object and can therefore make use of them and transfer their value to future calculations.

Why "degrees", and their total of 360, even exist doesn't have a clear answer. The theories range from being an invention of ancient astronomers who wanted to equate rotation with the (about) 360 rotations of the earth per year to Babylonian influence (whose number system was in base 60) to aliens.

I prefer the alien theory.

Point is, even though it's much easier to figure a movement in your head by using degrees ("I want to turn to my right, that's exactly 90 degrees!") and there's really nothing wrong with it, many functions can be made shorter and more effecient by switching to radians since they describes a relationship between the rotation and circle size at the same time.

Under constructions

Now what if you want to convert between the two forms of rotation measurement; degrees and radians? It's actually quite simple.

Let's say we have a degree of 60 we want in radians.

That formula, 1.046666666666667 = 3.14 * 60 / 180, is basically the same as 3.14 / 180 * 60, so we can just do the math and have 0.0174532925 * 60 to get the final radian rotation of 1.04666...

Luckily we don't have to do that math every time we want to convert one to another.



// Let's go back to some of the constants that we mentioned above.

// Conversion constants that are regularly used in geometry.

var RADIANS_TO_DEGREES = 180 / Math.PI;
var DEGREES_TO_RADIANS = Math.PI / 180.0;

// Now these two above, when exectuted by most computers, can return
// a practically endless length since Pi is involved.  For instance
// entering the math into a calculator might return figures like this.

var RADIANS_TO_DEGREES = 57.295779513082320876798154814105;
var DEGREES_TO_RADIANS = 0.0174532925199432957692369076848;

// We can shorten these values to a space more appropriate for
// floating point math.  Unless you are doing physical simulations
// in aeronautics or folding proteins for cancer research then
// chances are you can use the two shorthand values below to
// quickly convert radians to degrees and back again without
// sacrificing much noticeable accuracy.

var RAD_TO_DEG = 57.295779;
var DEG_TO_RAD = 0.0174533;

// After that, any time we want to convert one to another...

degrees = radians * RAD_TO_DEG;
radians = degrees * DEG_TO_RAD;

// This will become incredibly helpful later on.

Quaternions

xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx

Matrix Based Rotations

Guess what. This is so monumentally hard to wrap your head around it's going to take up an entire page all on it's own.

An ENTIRE PAGE.