7. Geometry & Trigonometry Essentials
Level: Beginner You'll use this for: GPS/location math, graphics, antenna/RF angles, and vector similarity (used constantly in ML/search).
Pythagorean theorem
a² + b² = c² for a right triangle. This generalizes directly to
Euclidean distance in n dimensions:
distance(P, Q) = sqrt( (p1-q1)² + (p2-q2)² + ... + (pn-qn)² )
import math
def euclidean(p, q):
return math.sqrt(sum((a-b)**2 for a, b in zip(p, q)))
euclidean([0,0], [3,4]) # 5.0 (classic 3-4-5 triangle)
euclidean([1,2,3], [4,6,3]) # distance in 3D
This is the similarity metric behind k-nearest-neighbors, clustering (k-means), and "find similar embeddings" in vector databases.
Sine, cosine, and the unit circle
For an angle θ on a unit circle: x = cos(θ), y = sin(θ). Radians, not
degrees, are the native unit in almost all math libraries (2π radians = 360°).
math.radians(180) # π
math.sin(math.pi/2) # 1.0
Cosine similarity — the workhorse of ML/search
cos_sim(A, B) = (A · B) / (|A| |B|)
Measures the angle between two vectors, ignoring their magnitude — used constantly to compare text/image embeddings ("how similar is this document to that one") because it cares about direction (meaning) not length (magnitude, which can vary with document length or brightness).
import numpy as np
def cosine_similarity(a, b):
a, b = np.array(a), np.array(b)
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
cosine_similarity([1,1,0], [1,0,0]) # ≈ 0.707 -> 45° apart
Computing that 0.707 by hand, three small steps:
A = [1, 1, 0] B = [1, 0, 0]
1. Dot product (A · B): multiply matching positions, sum them.
(1×1) + (1×0) + (0×0) = 1 + 0 + 0 = 1
2. Magnitudes (|A|, |B|): Pythagoras (Ch. 7 above), sqrt of sum of squares.
|A| = sqrt(1² + 1² + 0²) = sqrt(2) ≈ 1.414
|B| = sqrt(1² + 0² + 0²) = sqrt(1) = 1.0
3. Divide: cos_sim = 1 / (1.414 × 1.0) = 1 / 1.414 ≈ 0.707
cos(45°) ≈ 0.707 too — confirming the two vectors are 45° apart, which
checks out visually: [1,1,0] points diagonally, [1,0,0] points straight
along one axis.
The Haversine formula (great-circle distance on a sphere)
Straight-line (Euclidean) distance is wrong for lat/long coordinates because Earth is a sphere. The Haversine formula accounts for curvature:
def haversine(lat1, lon1, lat2, lon2):
R = 6371 # Earth radius in km
p1, p2 = math.radians(lat1), math.radians(lat2)
dp = math.radians(lat2 - lat1)
dl = math.radians(lon2 - lon1)
a = math.sin(dp/2)**2 + math.cos(p1)*math.cos(p2)*math.sin(dl/2)**2
return 2 * R * math.asin(math.sqrt(a))
haversine(40.7128, -74.0060, 51.5074, -0.1278) # NYC to London ≈ 5570 km
Real-life engineering ties
- SWE: map/location features, "nearby" search, geofencing.
- AI: cosine similarity for embedding search (RAG/vector DBs), image transforms (rotation matrices, Ch. 10).
- Network: RF/antenna beam angles, satellite link elevation angles, signal path geometry for line-of-sight microwave links.
- Security: anomaly detection on "impossible travel" (login from NYC, then Tokyo, 10 minutes later) uses Haversine distance ÷ time to flag physically implausible logins.
More real-life examples:
- GPS receivers compute your position via trilateration — solving a system of sphere-distance equations (a direct extension of the Pythagorean/Euclidean distance idea) using signals from 4+ satellites.
- Ride-share and mapping apps (Uber, Google Maps) combine Haversine "as the crow flies" distance with graph-based road-network distance (Ch. 17) to estimate ETAs and match nearby drivers.
- Music and video recommendation systems (Spotify, YouTube) compare embedding vectors with cosine similarity at massive scale — billions of comparisons a day resting on the same formula as the 3-line example above.
- Responsive UI/graphics layout (aspect ratios, scaling, rotation of elements on screen) is applied trigonometry and matrix transforms (Ch. 10) running in every browser's rendering engine.
Common mistake
Using Euclidean distance on latitude/longitude directly ("Pythagorean distance on lat/long") — it's wrong by an amount that grows with distance and gets worse near the poles, since a degree of longitude shrinks as you move away from the equator.