# Formula sheet: points on a sphere

You receive N points as (latitude, longitude) in degrees. This is the
standard geometry for placing them and measuring between them.

## Conventions

```
R     = 6371.0088 km          (the Earth here is a perfect sphere)
φ, λ  = latitude, longitude   (convert degrees → radians before any trig)
rad(d) = d · π / 180
```

## Great-circle distance (haversine)

The scorer's formula, exactly — same R, same operations. Reproduce it and
your local numbers match the leaderboard.

```
haversine_km(φ₁, λ₁, φ₂, λ₂):
    Δφ = φ₂ − φ₁
    Δλ = λ₂ − λ₁
    a  = sin²(Δφ/2) + cos(φ₁) · cos(φ₂) · sin²(Δλ/2)
    return 2 · R · asin( min(1, sqrt(a)) )
```

Two traps:

- The `min(1, …)` is load-bearing: round-off can push `sqrt(a)` just past 1
  for near-antipodal pairs, and `asin(>1)` is NaN.
- The law-of-cosines form (`acos(sin·sin + cos·cos·cos Δλ)`) is equivalent on
  paper but loses precision on short legs. Likewise, geodesic libraries
  (Vincenty, GeographicLib) model the real ellipsoidal Earth and will simply
  disagree with the scorer. Use the formula above.

## Scoring a tour

A tour is an ordering of all N points; its score is the open-path length —
the sum of consecutive legs, no return to start:

```
score(tour) = Σ haversine_km(tour[i], tour[i+1])    for i = 0 … N−2
```

## (lat, lng) → a point in space

Each point is also a unit vector from the globe's center to its surface:

```
x = cos φ · cos λ
y = cos φ · sin λ
z = sin φ
```
