1. Core Formulas
- 2D: d = √[(x₂−x₁)² + (y₂−y₁)²]
- 3D: d = √[(x₂−x₁)² + (y₂−y₁)² + (z₂−z₁)²]
- Midpoint: ((x₁+x₂)/2, (y₁+y₂)/2[, (z₁+z₂)/2])
- Vector: (Δx, Δy[, Δz]) = (x₂−x₁, y₂−y₁[, z₂−z₁])
2. Quick Decision Flow
- Same plane? Use 2D. Contains z? Use 3D.
- Need grid / steps? Consider Manhattan: |Δx| + |Δy|.
- Need max-axis move (chess king)? Chebyshev: max(|Δx|, |Δy|).
- Need direction? Pair distance with vector + atan2(Δy, Δx).
3. Sanity / Validation
- If one coordinate differs and others match → distance = absolute single difference.
- All coordinates equal → distance = 0.
- Large coordinates but tiny output? Re‑check subtraction order.
4. Shortcuts & Heuristics
- Pythagorean triples (3‑4‑5, 5‑12‑13…) speed mental checks.
- Factor out common scale: d = k·√(a²+b²).
- For many distances: precompute Δx, Δy arrays; reuse squares.
5. Common Pitfalls
- Forgetting square root (reporting d²).
- Swapping (x,y) order or mixing units.
- Using 2D formula on data with altitude/depth.
- Loss of precision with huge coordinates: use double and normalize if needed.
6. Micro Examples
(1,2) → (4,6): √[(3)²+(4)²] = 5
(1,2,3) → (4,6,7): √(3²+4²+4²)= √41 ≈ 6.4031
7. Mini FAQ
- Negative coordinates? Differences handle sign automatically.
- Order matter? No; squaring removes sign.
- Great‑circle? Use haversine; different model (not covered here).
8. Action Tip
When chaining many distances (clustering, kNN), cache squared distances if only comparisons are needed—skip the expensive √ until the final presentation.