Keywords

Fig. 1.
figure 1

Time-lapse images of multiple objects moving through spherical (a), planar (b) and hyperbolic (c) 2D space calculated and rendered by the described software

Fig. 2.
figure 2

Comparison of parallel lines in the 2D spaces

1 Introduction

Non-Euclidean geometry is a field that studies any space that arises from changing Euclid’s fifth postulate [1] or changing the metric requirement. In spherical geometry, Fig. 2(a), all geodesics (shortest paths in a non-planar space) intersect: don’t preserve the distance and appear to ‘bend’ towards each other. In Hyperbolic geometry, Fig. 2(c), each line has an infinite number of parallel lines, as they appear to ‘bend’ away from each other.

Fig. 3.
figure 3

Point A with polar coordinates r and \(\theta \)

We present a method for calculating the object’s position and its vertices in polar coordinates using spherical [2] or hyperbolic trigonometry [3, 4]. A polar coordinate system of the form \((r,\theta )\) is used in this model for all calculations instead of Cartesian coordinates. The centre of the of the screen is taken as a reference point O(0, 0) for the distance coordinate, r, while eastbound is the reference direction for the bearing coordinate, \(\theta \). This allows the same coordinates to be used irrespective of the correct curvature. In order to render the curved space onto a flat 2D screen, we are using azimuthal equidistant projection. By definition, distances and bearing from the centre of the projection are preserved. This works well with Polar coordinates, projection is intuitive and can be used with no changes for both spherical and hyperbolic 2D spaces (Fig. 3).

Fig. 4.
figure 4

O (0, 0), reference point; C \((r_c, \theta _c)\), position and local reference point; V\(_x\) \((r_x,\theta _x)\), vertices; OO’, reference direction; CC’, local reference direction

2 Method

The calculations are split into two parts: movement of the objects and rendering of the shapes. The screen (rendering space) is limited to a circle of an arbitrary size. When the object’s centre moves past the circumference of the circle, it is repositioned to the antipodal point on the circle with the velocity preserved. This is implemented in order to keep the objects in the visible area on the screen.

Shape has a list of position vectors for each vertex in local coordinates with object’s position being the reference point and reference direction is taken as the reverse of its position vector (Fig. 4).

2.1 Rendering the Shape

Let \(K \in [-1,1] \subset \mathfrak {R}\) s.t. \(K = 0 \Rightarrow \) Euclidean Geometry;

Fig. 5.
figure 5

Spherical triangle

\(K > 0 \Rightarrow \) Spherical Geometry, \(r = \frac{1}{\sqrt{K}}\)

Theorem 1

For a sphere of radius r and hence Gaussian curvature \(K = \frac{1}{r^2}\) and a spherical triangle on its surface described by points u, v and w, connected by great circles that form the edges a, b, c (interpreted as subtended angles) and an angle C (Fig. 5), the spherical law of cosines [5] states:

$$\begin{aligned} \cos {\frac{c}{r}} = \cos {\frac{a}{r}} \cos {\frac{b}{r}} + \sin {\frac{a}{r}} \sin {\frac{b}{r}} \cos {C} \end{aligned}$$
(1)
Fig. 6.
figure 6

Hyperbolic triangle

\(K < 0 \Rightarrow \) Hyperbolic Geometry, \(k = - \frac{1}{\sqrt{K}}\)

Theorem 2

For a hyperbolic plane with Gaussian Curvature \(K = -\frac{1}{k^2}\) and a hyperbolic triangle on its surface described by points u, v and w, connected by geodesics that form the edges a, b and c, as well as an angle C (Fig. 6), the hyperbolic law of cosines [6] states:

Fig. 7.
figure 7

Finding the \(\theta \) and r coordinates of an object’s vertices through a hyperbolic/spherical triangle OCV; Case (a): \(\theta _{local} + \alpha < \pi \); case (b): \(\theta _{local} + \alpha > \pi \)

$$\begin{aligned} \cosh {\frac{c}{k}} = \cosh {\frac{a}{k}} \cosh {\frac{b}{k}} - \sinh {\frac{a}{k}} \sinh {\frac{b}{k}} \cos {C} \end{aligned}$$
(2)

Note: To simplify the equations below, all lengths are assumed to have been divided by r or k depending on the value of K.

Corollary 1

Given:

Find: \(r_v\), \(\theta _v\) = ?

If \(K > 0\), then:                         If \(K < 0\), then:

$$\begin{aligned} \begin{aligned} \cos r_v =&\cos { r_c} \cos {r_{local}} + \\&\sin {r_c}\sin {r_{local}} \cos {\beta } \end{aligned}&\begin{aligned} \cosh r_v =&\cosh { r_c} \cosh {r_{local}} - \\&\sinh {r_c}\sinh {r_{local}} \cos {\beta } \end{aligned} \end{aligned}$$
(3)
$$\begin{aligned} \begin{aligned} \cos \varDelta \theta _v =&\frac{\cos {r_{local}}-\cos {r_c} \cos {r_v}}{\sin {r_c} sin{r_v}} \end{aligned}&{\begin{aligned} \cos \varDelta \theta _v =&\frac{\cosh {r_c} \cosh {r_v}- \cosh {r_{local}}}{\sinh {r_c} \sinh {r_v}} \end{aligned}} \end{aligned}$$
(4)

In order to find \(r_v\), first find \(\beta = \alpha + \theta _{local}\); if \(\varPi< \beta < 2\varPi \), use the explementary angle instead to determine to which side of OC the triangle lies. Depending on that \(\varDelta \theta \) is then added to or subtracted from \(\theta _c\) to find \(\theta _v\) (Fig. 7).

Fig. 8.
figure 8

Finding the length of edge d and the angle \(\varDelta \theta \). Case (a), \(\varDelta \theta _1\) and \(\varDelta \theta _2\) diverge, so \(\varDelta \theta \) is the sum; case (b), angles converge, so \(\varDelta \theta \) is the absolute value of the difference.

Corollary 2

Given: (Fig. 8)

Find: d, \(\varDelta \theta \) = ?

If angles converge, \(\varDelta \theta = ||\varDelta \theta _1 - \varDelta \theta _2 ||\); if angles diverge, \(\varDelta \theta = ||\varDelta \theta _1 ||+ ||\varDelta \theta _2 ||\)

If \(K > 0\), then:                         If \(K < 0\), then:

$$\begin{aligned} \begin{aligned} \cos d =&\cos { r_1} \cos {r_2} + \\&\sin {r_1}\sin {r_2} \cos {\varDelta \theta } \end{aligned}&\begin{aligned} \cosh d =&\cosh {r_1} \cosh {r_2}- \\&\sinh {r_1}\sinh {r_2} \cos {\varDelta \theta } \end{aligned} \end{aligned}$$
(5)
Fig. 9.
figure 9

Finding intermediate points in order to render the edge.

Note: distance d is divided into a number of equal parts in order to find the distance \(d_i\) for each of the points on the edge V\(_1\)V\(_2\). The number of segments depends on the object tesselation variable.

Corollary 3

Given:

Find: \(r_i\), \(\theta _i\) = ?

If \(K > 0\), then:                                        If \(K < 0\), then:

(6)
(7)
(8)

\(\alpha \) is calculated to find the angle opposite r\(_i\). Then \(r_i\) and subsequently \(\varDelta \theta _i\) can be found using the cosine rule (illustrated on Fig. 9). Then to find actual coordinates of the point \(V_i\), \(r_i\) should be multiplied by r or k depending on the value of K; \(\varDelta \theta _i\) should be added to or subtracted from angle \(\theta \)1, depending on the direction of the edge d, determined previously.

Fig. 10.
figure 10

Movement of the object along a hyperbolic in Spherical (a) and Hyperbolic (b) space. Orientation with respect to the geodesic is kept the same (angle \(\alpha \) is constant) if the object is not rotating.

2.2 Updating Object Position

Corollary 4

Given:

Find: \(r_{t1}\), \(\theta _{t1}\), \(\gamma _{t1}\), \(\beta _{t1}\) = ?

\(\gamma _{t0}\) should be 0 to \(\pi \), if calculated value is \(\gamma _{t0} > \pi \), take the explemntary angle. This indicates the movement direction with respect to the reference point (Fig. 10).

Let \(\angle OC_{t1}C_{t0} = \gamma '_{t1}\)

If \(K > 0\), then: If \(K < 0\), then:

(9)
(10)
(11)

\(\alpha = \beta _{t0} - \gamma _{t0}\). \(\alpha \) is the difference between rotation direction and the geodesic of movement (\(C"\!_0{C"\!_1}\)), it does not change if the object is not rotating. Hence, \(\beta _{t1} = \gamma _{t1} + \alpha \). Because \(\gamma '_{t1}\) and \(\gamma _{t1}\) are supplementary angles, \(\gamma _{t1} = \varPi - \gamma '_{t1}\).

To find the \(\theta \) coordinate, either subtract or add \(\varDelta \theta \) to the \(\theta _c\) depending on whether the angle \(\alpha \) or its explementary angle is used for this calculation.

3 Results

3.1 Implementation

Using the method described above and OpenGL, we created a software capable of calculating the objects and rendering the vector graphics in a non-Euclidean space with constant curvature in the range of \(-1\le K \le 1\). Figure 1 shows the time-lapses of multiple objects in spherical (a), planar (b) and hyperbolic (c) geometries. They show movement through different geodesics at \(K=1\), \(K=0\) and \(K=-1\) respectively. Starting positions as well as shape definitions of each object are the same across all time-lapses (grid-lines have been created and rendered as separate objects). The software can calculate the object moving in arbitrary direction with arbitrary speed as well as starting from arbitrary position in the space.

Curvature of the world can be modified in real-time using keyboard inputs in a similar manner to controlling the object’s acceleration and orientation. Another feature is the cut-off of the world at a distance of N pixels. This can be seen in the hyperbolic and planar time-lapse images. While these spaces should be infinite, we chose to limit them in order to keep objects within the boundaries of the screen (non-shaded area). We created a video [7] displaying the implementation.

3.2 Complexity Analysis

Positions of each vertex need to be calculated, requiring O(v) time, where v is the number of vertices. Subsequently, intermediate points have to be computed, requiring O(i) time to find all of the points on a single edge, where i is the level of tessellation. Complexity to render the world with s number of shapes is therefore \(O(s*v*i)\). The best case would be equal to O(n) complexity, if two of the terms are negligibly small. The worst case can be approximated to \(O(n^3)\) if all terms were comparably large. Spatial complexity for shape rendering is only \(O(v*i)\) as previous shape’s data is rewritten to store the next shape’s data. So either O(n) in the best case or \(O(n^2)\) in the worst case.

Only one movement calculation per object is required and the previous position record is overwritten, both spatial and time complexity is O(n), where n is the number of objects in the world.

Trigonometric and hyperbolic functions in the calculations are slower to compute than simpler operations, hence additional cost (implementation dependent). For example the AGM iteration [8] method is faster than the previously common Taylor series method.

4 Discussion

Implementation does not affect performance up to a certain number of objects or tessellation amount. The focus was on implementing the method correctly and having it work continuously under any curvature in the range \(-1 \le K \le 1\).

The next step in the project’s development is improving the execution time using parallelised calculations. Subsequent calculation of the points creates a bottleneck, which can be solved by performing some calculations directly on the GPU. Other approaches are considered as well, including lookup tables to speed up trigonometric calculations, for example, Frank Rochet’s implementation [9]; or finding intermediate points from a geodesic equation.

Potential applications for the software include education about non-Euclidean geometry (more intuitive than standard projections: Poincare disk and Upper Half-Plane models); cartography [10] (the engine could be modified to efficiently convert data into different projections); ecology [11] and climatology [12] (modelling dynamic systems); Astrophysics (modelling systems of cosmological objects and gravitational fields) and video games (game engine for a real-time continuous non Euclidean space, unlike HyperRogue [13], which uses step by step implementation).