# Living on the n-sphere

By: Gijs Bellaard

## Definitions

We define the n-sphere \(S^n\) as all the points in \(\bbR^{n+1}\) with unit euclidean length:
\[ S^n := \{ x \in \bbR^{n+1} \mid ||x|| = 1 \}\]
A great circle \(C\) is defined as the intersection of the n-sphere with a plane \(H \subseteq \bbR^{n+1}\):
\[ C(H) = S^n \cap H \]
By writing \(H\) as the span of two vectors \(a, b \in S^n\) that are orthogonal \(\Ang{a,b} = 0\), where
\(\Ang{\cdot, \cdot}\) is the standard dot product, allows us to parametrize \(C\):
\[ C(a, b) = \{ a \cos t + b \sin t \mid t \in [0, 2\pi ]\}\]
One can confirm for himself, as an exercise, that this parametrization is indeed correct.
The angle \(\alpha\) between two vectors on the n-sphere is * exactly * the distance between them on the n-sphere.
Using this the dot product can also be written as
\[\Ang{x,y} = ||x|| \cdot ||y|| \cdot \cos \alpha = 1 \cdot 1 \cdot \cos d(x,y) = \cos d(x,y)\]
which reveals the metric on the n-sphere to be
\[ d(x,y) = \arccos \Ang{x,y}\]
again, one can confirm this to be an actual metric by himself.
Now that we have a metric we can define a ball \(B\) on the n-sphere to be all the points that are at most some distance \(r \leq \pi\) away from a center \(c\):
\[ B(c, r) := \{ x \in S^n \mid d(x,c) \leq r \} = \{ x \in S^n \mid \Ang{x,c} \geq \cos r \}\]
We can also define the "great donut" \(D\) as all the points that are at most some distance \(r \leq \pi/2\) away from a great circle \(C\):
\[ D(C, r) := \{ x \in S^n \mid d(x,C) \leq r\}\]
where the distance to a set \(\Omega\) is simply defined as:
\[d(x,\Omega) := \inf_{o \in \Omega} d(x,o)\]
The distance/angle \(\alpha\) between the great circle \(C(a,b)\) and \(x\)
is the same as the angle between \(x\) and its projection \(p\) onto \(H(a,b)\).
This means that, together with the fact that the origin, \(x\) and \(p\) form a right angle triangle, that:
\[ \cos \alpha = ||p|| = \sqrt{\Ang{x,a}^2 + \Ang{x,b^2}} \]

this allows us to rewrite the distance to a great circle as: \[ d(x, C) = \arccos \sqrt{\Ang{x,a}^2 + \Ang{x,b}^2} \] All in all, this means a great donut is also: \[ D(a,b,r) := \{ x \in S^n \mid \Ang{x,a}^2 + \Ang{x,b}^2 \geq \cos^2 r\}\]

## Intersectors

A ray \(r(t)\) on the n-sphere which is basically just a parametrized great circle: \[ r(t) := o \cos t + d \sin t \] where we interpret \(o\) as the start(origin) of the ray and \(d\) as the direction it's going, with again \(o,d \in S^n\) and \(\Ang{o,d}=0\).

### Ball

We are interested in when a ray intersects a ball \(B(c,r)\): \[ r(t) \in \partial B\] where \(\partial B\) is the boundary of \(B\). By definition of the ball this is equivalent to: \[ \Ang{r(t),c} = \cos r \] which can be expanded to: \[ \Ang{o,c} \cos t + \Ang{d,c} \sin t = \cos r \] this is an equation of the form \(A\cos t + B\sin t = C\) which can be solved analytically. How this is done exactly will be explained later.

### Great Donut

We are interested in when a ray intersects a great donut \(D(a, b, r)\): \[ r(t) \in \partial D\] By definition of the great donut this is equivalent to: \[ \Ang{r(t),a}^2 + \Ang{r(t),b}^2 = \cos^2 r \] We introduce \(\tilde{o} := ( \Ang{o, a}, \Ang{o, b} ) \in \bbR^2 \) and similarly \(\tilde{d}\). The equation above can then be expanded to: \[ \Ang{\tilde{o},\tilde{o}}\cos^2 t + 2\Ang{\tilde{o},\tilde{d}}\cos t \sin t + \Ang{\tilde{d},\tilde{d}}\sin^2 t = \cos ^2 r \] This is an equation of the form \(A\cos^2 t + 2B\cos t\sin t + C \sin^2 t= D\) which can be solved analytically. How this is done exactly will be explained later.

## Solving Trigonometric Equations

### The one of the Ball

We are trying to solve the following equation for \(t\): \[ A\cos t + B\sin t = C \] The trick is to interpret \((B,A)\) as a point in \(\bbR^2\) and write it in polar coordinates: \[ B = r \cos \theta, \quad A = r \sin \theta\] substituting this back in the equation gives: \[ r(\sin \theta \cos t + \cos \theta \sin t) = C\] applying the sum formula for sine: \[ r \sin(\theta + t) = C \] and so: \[ t = \arcsin(C/r) - \theta\] Of course this equation doesn't always allow for a solution, this happens when \(|C/r| > 1\).

### The one of the Great Donut

We are trying to solve the following equation for \(t\): \[ A\cos^2 t + 2B\cos t\sin t + C \sin^2 t= D \] using: \[ \cos^2t = \frac{1 + \cos 2t}{2}, \quad \sin^2 t = \frac{1 - \cos 2 t}{2}, \quad 2 \cos t \sin t = \sin 2 t \] we can transform this equation into one of the form: \[ A\cos u + B\sin u = C \] where \(u = 2t\), which we can solve as seen above.

## Spherical Universe

One might have heard about the idea that the universe is not flat be spherical, which means the universe is like \(S^3\). I was wondering how this would actually look so I made the following shader using the math described here. To control the camera use WASD+QE+Space+Shift+Mouse.