Polar Rotation

From Graal Bible

Definitions

θ: greek letter theta
φ: greek letter phi
theta and phi are commonly used in trigonometry for angles (but phi is most commonly used as a constant: 1.618...).
α: 1st greek letter alpha. For our purposes, it will represent rotation about the y-axis.
β: 2nd greek letter beta. For our purposes, it will represent rotation about the x-axis.
γ: 3rd greek letter gamma. For our purposes, it will represent rotation about the z-axis.

sin(θ) = y / r (Δy of a point from the center on a circle with radius r)
cos(θ) = x / r (Δx of a point from the center on a circle with radius r)
sin(θ ± φ) = sin(θ) cos(φ) ± cos(θ) sin(φ)
-cos(θ ± φ) = -cos(θ) cos(φ) ± sin(θ) sin(φ)

In addition (not really useful):

tan(θ) = sin(θ) / cos(θ)
cot(θ) = cos(θ) / sin(θ)
sec(θ) = 1 / cos(θ)
csc(θ) = 1 / sin(θ)

Deriving the 2D equations

Simply:

x² + y² = r²; the basic equation of a circle with center (0,0)
x² + y² + z² = r²; the basic equation of a sphere with center (0,0,0)
sin²(θ) + cos²(θ) = 1
arctan(y/x) = θ; arctan() being the inverse of tangent
tan(arctan(y/x)) = tan(θ)
y/x = tan(θ)
y/x = r sin(θ) / r cos(θ)

Thus,

r sin(θ) = y
r cos(θ) = x

Likewise,

sin(θ) = y / r
cos(θ) = x / r

Plugging into the circle equation, we get

r² = r² cos²(θ) + r² sin²(θ)

Compare:

√(a² + b²) = c (the distance formula)
x² + y² = r² (established as the equation of a circle)
√(a² + b²) = c
( √(a² + b²) )² = c²
a² + b² = c²

Rotation on a single plane

Think of a circle with some radius, r, and a point somewhere on it. This circle is a two-dimensional figure on the xy [or y(x)] plane. The angle between 0º and the point is θ.
Trigonometric functions sine and cosine are specific to determine x and y values according the the angle passed as its parameter.

To keep things simple, we will rotate about the (invisible) z-axis. In doing so, x and y are changing. Any z values that a point may have remain constant. φ is a second angle, representing the amount of rotation.

y = r sin(θ), so,
y = r sin(θ + φ)
y = r ( sin(θ) cos(φ) + cos(θ) sin(φ) )
y = r sin(θ) cos(φ) + r cos(θ) sin(φ)

Using substitutions form above, we can simplify to

y = y cos(φ) + x sin(φ)

Alternatively,

x = r cos(θ)
x = r cos(θ + φ)
x = r ( cos(θ) cos(φ) - sin(θ) sin(φ) )
x = r cos(θ) cos(φ) - r sin(θ) sin(φ)
x = x cos(φ) - y sin(φ)

Deriving the 3D equations

This image shows how to determine the cartesian coordinates of a point on a sphere in terms of sine and cosine. The point is at the upper tip of the green triangle.

As above, we can change the sphere's equation into a series of distance formulas

x² + y² + z² = r²
( √(x² + y²) )² + z² = r²	 
( √( ( √(x² + y²) )² + z² ) )² = r²
√( x² + y² + z² ) = r
r√( ( sin²(θ) + cos²(θ) ) sin²(φ) + cos²(φ) ) = r
√( ( √( sin²(θ) + cos²(θ) ) )² sin²(φ) + cos²(φ) ) = 1
( sin²(θ) + cos²(θ) )² sin²(φ) + cos²(φ) = 1
sin²(θ) sin²(φ) + cos²(θ) sin²(φ) + cos²(φ) = 1

Comparing with the original equation we can split it up and get individual equations for x,y, and z.

y = sin(θ) sin(φ) (1)
x = cos(θ) sin(φ) (1)
z = cos(φ)

The equation is

( sin²(θ) + cos²(θ) ) ( sin²(φ) + cos²(φ) ) = 1

So, why doesn't the ( sin²(θ) + cos²(θ) ) distribute to the cos²(φ)? If you'll recall, ( sin²(θ) + cos²(θ) ) = 1, so it could be that it was. But also, it was left out so that there were only 3 terms as opposed to 4 (We only want three: x, y, and z!).


(1) The portion used contained the original x or y equation, so was split up to match, but it doesn't matter in the end.

Rotation on three planes

Now in three dimensions:
Imagine three planes that intersect: xy [y(x)], yz [z(y)], zx [x(z)] (using these names helps remember the order of calculations if re-writing the script... you'll see).
For this, instead of merely one rotation angle, we will need three (α, β, and γ). Also, we will need to replace the simple x and y with their three-dimensional counterparts and add a z (To begin, f = f0, where f is a function, x, y, or z).

x0 = r sin(α) cos(β)
y0 = r sin(α) sin(β)
z0 = r cos(α)

About the x-axis:
Finally, rotate our three planes to replace z with x, y with z, and x with y.

nz = z cos(β) + y sin(β)
ny = y cos(β) - z sin(β)

Note how as the axes are switched, the x, y, and z or replaced respectively from the 2D rotation equations!

We then replace y with ny and z with nz. So in a sense,

z = nz
y = ny

About the y-axis:
Now imagine the 3 planes rotate forward 90º and 90º to the left. The z has taken the place of the x, x in the stead of y, and y in that of z. The idea of this rotation is now just like that above, but y is constant.

nx = x cos(α) + z sin(α)
nz = z cos(α) - x sin(α)
x = nx

The two-dimensions calculations work for rotations about the z-axis.
About the z-axis:

ny = y cos(γ) + x sin(γ)
nx = x cos(γ) - y sin(γ)

We expand to simplify:

x = x cos(γ) - y sin(γ)
x = ( x0 cos(α) + ( z0 cos(β) + y0 sin(β) ) sin(α) ) cos(γ) - ( y0 cos(β) - z0 sin(β) ) sin(γ)
x = x0 cos(α) cos(γ) + z0 cos(β) sin(α) cos(γ) + y0 sin(β) sin(α) cos(γ) - y0 cos(β) sin(γ) + z0 sin(β) sin(γ)
x = x0 cos(α) cos(γ) + y0 ( sin(β) sin(α) cos(γ) - cos(β) sin(γ) ) + z0 ( cos(β) sin(α) cos(γ) + sin(β) sin(γ) )
y = y cos(γ) + x sin(γ)
y = ( y0 cos(β) - z0 sin(β) ) cos(γ) +  ( x0 cos(α) + ( z0 cos(β) + y0 sin(β) ) sin(α) ) sin(γ)
y = y0 cos(β) cos(γ) - z0 sin(β) cos(γ) + x0 cos(α) sin(γ) + z0 cos(β) sin(α) sin(γ) + y0 sin(β) sin(α) sin(γ)
y = x0 cos(α) sin(γ) + y0 ( cos(β) cos(γ) + sin(β) sin(α) sin(γ) ) + z0 ( -sin(β) cos(γ) + cos(β) sin(α) sin(γ) )
z = z cos(α) - x sin(α)
z = ( z0 cos(β) + y0 sin(β) ) cos(α) - x0 sin(α)
z = z0 cos(β) cos(α) + y0 sin(β) cos(α) - x0 sin(α)

As is, the object will display in an orthographic projection, which means that z has no bearing on the perspective. To give a perspective projection, the x and y can be distorted by some equation like:

x = centerx + h / (h-z)
y = centery - h / (h-z)

Links

Amon-ra's explanation and models (continues on p. 2)