Topic

Spherical geometry

On a sphere the straight lines are the great circles. Any two of them cross, the angles of a triangle add up to more than two right angles, and the surplus is the triangle's area. Here the points have exact coordinates, so those statements are equalities, not measurements.

const A = spoint(1, 0, 0), B = spoint(1, 1, 0), C = spoint(1, 1, 1);   // any non-zero vector, scaled onto the sphere
A.distanceTo(B).text;                     // "π/4": the distance is the angle between them
const t = striangle(A, B, C);
[t.angles.A.text, t.angles.B.text, t.angles.C.text];   // ["π/4", "π/2", "π/3"]
t.excess.text;                            // "π/12": the angle sum is π + π/12
String(t.fractionOfSphere);               // "1/48"
t.checks().all;                           // true: every law of spherical trigonometry, exactly
greatCircle(A, B).meet(greatCircle(A, C));   // two opposite points: there are no parallels

Points and distance

A point is a unit vector (x,y,z)(x, y, z) with x2+y2+z2=1x^2 + y^2 + z^2 = 1, and the coordinates are exact real algebraic numbers. spoint(1, 1, 1) scales a vector onto the sphere, giving (33,33,33)(\tfrac{\sqrt3}{3}, \tfrac{\sqrt3}{3}, \tfrac{\sqrt3}{3}); latLon(lat, lon) takes a latitude and longitude as fractions of a full turn; and fromStereographic([X, Y]) lifts a point of the plane, which turns rational points of the plane into rational points of the sphere, such as (3649,2449,−2349)(\tfrac{36}{49}, \tfrac{24}{49}, -\tfrac{23}{49}).

The distance between two points is the angle between the vectors, measured along the great circle through them, and its cosine is the dot product:

cos⁡d(P,Q)=P⋅Q. \cos d(P, Q) = P \cdot Q.

The cosine is exact. The distance itself is an Angle.

Angles that are, and are not, fractions of π

An Angle keeps its cosine and sine exactly. Its turns is the angle as a fraction of a full turn when it is a rational multiple of π, and null when it is not, and the answer is decided, not guessed. The reason it can be decided: cos⁡(2πk/n)\cos(2\pi k/n) is an algebraic number of degree φ(n)/2\varphi(n)/2 (Euler's function), so the degree of a cosine leaves only finitely many denominators nn to try, and each is tested with an exact equality. In particular a rational cosine can only be 0,±12,±10, \pm\tfrac12, \pm1 (Niven's theorem), so the angle with cosine 13\tfrac13 is not a rational multiple of π, and the library says so with a proof behind it.

When the angle is such a multiple, text is of the form "π/12". Otherwise value(digits) gives an interval of radians that contains it. Angles add as real numbers (a float tracks which turn the sum is on), so an angle sum of 13π/1213\pi/12 is that and not the same thing modulo 2π2\pi.

Great circles

A great circle is where a plane through the centre cuts the sphere. It is kept as a vector nn normal to that plane: the circle is {P:n⋅P=0}\{P : n \cdot P = 0\}. Through two points that are neither equal nor opposite there is exactly one, with n=P×Qn = P \times Q. Two great circles always meet, in the two opposite points ± n1×n2\pm\, n_1 \times n_2. So on the sphere there are no parallel lines, and two "lines" meet twice, which is why two points do not always determine a line: through two opposite points there are infinitely many, and greatCircle refuses them.

Triangles and their excess

Three points not on one great circle make a triangle. Its sides a,b,ca, b, c are angles (the distances between the vertices), and its angles A,B,CA, B, C are the angles between the planes of the sides. Their sum is always more than π, and the surplus

E=A+B+C−π E = A + B + C - \pi

is the spherical excess. Girard's theorem says the area of the triangle on the unit sphere is exactly EE. The octant triangle with vertices on the three axes has three right angles, excess π/2\pi/2, and covers one eighth of the sphere's 4π4\pi. The triangle in the example at the top has angles π/4,π/2,π/3\pi/4, \pi/2, \pi/3 and covers one forty-eighth: the 48 symmetries of a cube move it over the whole sphere.

When the angles are not fractions of π the excess usually is not one either, and it comes as an enclosure. checks() also tests it against a formula that needs no angles at all,

tan⁡E2=∣A⋅(B×C)∣1+A⋅B+B⋅C+C⋅A, \tan\frac E2 = \frac{|A \cdot (B \times C)|}{1 + A\cdot B + B \cdot C + C \cdot A},

as an exact equality of algebraic numbers.

Spherical trigonometry

The plane's law of cosines has two forms on the sphere:

cos⁡a=cos⁡bcos⁡c+sin⁡bsin⁡ccos⁡A,cos⁡A=−cos⁡Bcos⁡C+sin⁡Bsin⁡Ccos⁡a. \cos a = \cos b \cos c + \sin b \sin c \cos A, \qquad \cos A = -\cos B \cos C + \sin B \sin C \cos a.

The second has no counterpart in the plane: it gives the sides from the angles, so on a sphere there are no similar triangles of different sizes. The law of sines is sin⁡A/sin⁡a=sin⁡B/sin⁡b=sin⁡C/sin⁡c\sin A / \sin a = \sin B / \sin b = \sin C / \sin c. The polar triangle, whose vertices are the poles of the sides, has sides π−A,π−B,π−C\pi - A, \pi - B, \pi - C. checks() tests all of these on the triangle given, each as an exact equality.

Circles

A circle of radius rr (an angle) about a point has circumference 2πsin⁡r2\pi \sin r and encloses a cap of area 2π(1−cos⁡r)2\pi(1 - \cos r): both less than the plane's 2πr2\pi r and πr2\pi r^2. They come as exact multiples of π: the circle of radius π/6\pi/6 has circumference exactly π and area (2−3)π(2 - \sqrt3)\pi.

Isometries

The isometries of the sphere are the orthogonal 3 × 3 matrices. srotation(axis, turns) is the rotation about an axis, and classify() reads any isometry back: by Euler's theorem every isometry that keeps orientation is a rotation about some axis, so the product of two rotations about different axes is a third rotation, and the library finds its axis and angle exactly. The ones that reverse orientation are reflections in great circles and rotary reflections, the antipodal map among them.

callreturns
spoint(x, y, z), latLon(lat, lon), fromStereographic(p), northPole()SPoint: x, y, z, antipode, distanceTo, dot, latLon, stereographic
greatCircle(P, Q), greatCircle(pole), equator()GreatCircle: contains, side, poles, meet, angleWith, perpendicularThrough, distanceTo, reflection, parallelsThrough
striangle(A, B, C)STriangle: sides, angles, angleSum, excess, area, fractionOfSphere, perimeter, polar, classify(), checks()
scircle(centre, point | { radius } | { cos })SCircle: radius, circumference, area, contains, isGreat
srotation(axis, turns), antipodalMap(), circle.reflectionSIsometry: apply, then, inverse, classify()
angle(turns | { cos } | { cos, sin }), Anglecos, sin, turns, text, degrees, value(digits), add, sub, equals
turnsOfCos(c), piTimes(k), lune(angle)The fraction of a turn with that cosine, or null; π times an exact number; a lune's area
Try itEdit the code and run it with @ratmath/geo and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs