programdemos/sphere-hello/sphere-hello.xtl

Sphere hello: a handful of points on the unit sphere. Two ways to measure how far apart two of them are, the straight line through the sphere and the great circle along it, order every point's neighbors the same way; and the plain average of the points is not on the sphere, where the mean along it is.

source · imports mf: libs/Manifold/src/Manifold.xtl; ch: work/xetal-libraries/libs/Check/src/Check.xtl work/xetal-libraries/libs/Check/src/Check.xtlm

The points

P : Float

value · line 14

Six directions in three dimensions, chosen by hand (nothing is random), each put onto the unit sphere: one point per row.

P ← ᵐᶠs̲phereProject 6 3 r̲eshape 1.0 0.2 0.1 0.9 0.5 0.0 0.3 1.0 0.2 -0.4 0.8 0.5 0.1 0.3 1.0 -0.6 -0.5 0.6

Two distances

ᵘc̲hords : Float -> Float

function · line 22

The straight line through the sphere (the chord) between every pair: the length of the difference, summed one coordinate at a time.

ᵘc̲hords ← { Q →
  n ← f̲irst s̲hape Q
  (D, _) ← (2 s̲elect s̲hape Q) '{ (d, k) → c ← k s̲elect₂ Q◆ (d + (c '− t̲able c) ^ 2.0, k + 1) } p̲ower ((n c̲at n) r̲eshape 0.0, 1)
  s̲qrt D
}
Used in: chord

chord : Float

value · line 27
chord ← ᵘc̲hords P

arc : Float

value · line 32

The great circle along the sphere between every pair: the angle, in radians (Manifold).

arc ← ᵐᶠs̲phereDistances P

The same neighbors

ᵘo̲rders : Ord a => a -> Int

function · line 50

Each point's neighbors, nearest first (itself first of all, at distance 0): one row per point, the points' numbers in order of that row of the distance matrix.

ᵘo̲rders ← { D →
  n ← f̲irst s̲hape D
  (R, _) ← n '{ (acc, i) → (acc c̲at (1 c̲at n) r̲eshape g̲rade i s̲elect D, i + 1) } p̲ower ((0 c̲at n) r̲eshape 0, 1)
  R
}
Used in: byChord, byArc

byChord : Int

value · line 55
byChord ← ᵘo̲rders chord

byArc : Int

value · line 57
byArc ← ᵘo̲rders arc

Two means

plain : Float

value · line 64

The plain mean of the points, coordinate by coordinate: it lies inside the sphere, and its length says how far in.

plain ← ('+ r̲/ P) ÷ 6.0

onSphere : Float

value · line 72

The mean on the sphere (Manifold): the point along the sphere whose summed squared arcs to the points is least. It is on the sphere, and it is not the plain mean put onto it.

onSphere ← ᵐᶠs̲phereMean P