Calculating distance and compass bearing between two points on Earth using vectors, spherical coordinates, and trigonometry — based on a Multivariable Calculus final project.
Navigation systems such as GPS, Google Maps, and aviation software calculate distances and directions between locations almost instantly. Although these calculations look simple to users, they rely on mathematical concepts including vectors, spherical coordinates, trigonometry, and geometry. Because the Earth is approximately spherical, standard two-dimensional distance formulas (like the Pythagorean theorem) are not sufficient for accurately measuring long distances between two locations.
This site walks through the full derivation — from latitude/longitude to 3D position vectors to dot products — and lets you calculate the great-circle distance and initial compass bearing between any two locations on Earth.
Every location on Earth can be described using latitude ($\phi$) and longitude ($\lambda$). Latitude measures how far north or south a point is from the equator ($-90^\circ$ to $90^\circ$), while longitude measures how far east or west a point is from the Prime Meridian ($-180^\circ$ to $180^\circ$). Together, $(\phi, \lambda)$ act like spherical coordinates on a sphere of radius
$$R = 6371 \text{ km (mean Earth radius)}$$
Because trigonometric functions expect radians, every angle must first be converted:
$$\text{radians} = \text{degrees} \times \frac{\pi}{180}$$
Vectors are useful here because they let us treat two points on a curved surface as ordinary points in $\mathbb{R}^3$, where dot products, angles, and magnitudes are easy to compute. Each location is converted into a position vector using the spherical-to-Cartesian conversion:
$$X = R\cos(\phi)\cos(\lambda) \qquad Y = R\cos(\phi)\sin(\lambda) \qquad Z = R\sin(\phi)$$
This produces vectors $\vec{A} = (X_1, Y_1, Z_1)$ and $\vec{B} = (X_2, Y_2, Z_2)$ representing the two cities as points on the Earth's surface.
The dot product formula relates two vectors and the angle between them:
$$\vec{A} \cdot \vec{B} = |\vec{A}||\vec{B}|\cos(\alpha)$$
Solving for the angle gives the central angle — the angle formed at the center of the Earth between the two location vectors:
$$\alpha = \cos^{-1}\left(\frac{\vec{A}\cdot\vec{B}}{|\vec{A}||\vec{B}|}\right)$$
The shortest path between two points on a sphere is called the great-circle distance. Once the central angle is known, the arc length is simply:
$$d = R\alpha$$
This is why the great-circle distance is usually shorter than driving distance — roads bend around terrain, while a great circle is the true shortest path along the sphere's surface.
Distance alone doesn't tell you which direction to travel. The initial compass bearing $\theta$ (measured clockwise from true north) is given by:
$$\theta = \tan^{-1}\left(\frac{\sin(\Delta\lambda)\cos(\phi_2)}{\cos(\phi_1)\sin(\phi_2)-\sin(\phi_1)\cos(\phi_2)\cos(\Delta\lambda)}\right)$$
where $\Delta\lambda = \lambda_2 - \lambda_1$, normalized to a compass heading between $0^\circ$ and $360^\circ$.
| Quantity | State College | Philadelphia |
|---|---|---|
| Latitude | 40.793° | 39.953° |
| Longitude | -77.860° | -75.165° |
| Position Vector (km) | (1015, -4706, 4177) | (1243, -4738, 4087) |
Central angle: $\alpha \approx 0.0387$ rad $\approx 2.22^\circ$
Great-circle distance: $d \approx 246.6$ km $\approx$ 153.2 miles
Initial bearing: 113.5° (east-southeast)
Enter two locations (place names or coordinates) to calculate the great-circle distance and initial compass bearing between them, plotted on the map below.
This table updates live with whatever two locations you enter in the Calculator above. The great-circle distance is computed two independent ways — the vector/dot-product method from this project, and the classic Haversine formula — as a cross-check. The Google Maps row reflects the original State College → Philadelphia case study from the paper.
Showing: State College, PA → Philadelphia, PA
| Method | Distance (miles) | Bearing (°) |
|---|---|---|
| Vector / Dot Product (this calculator) | 153.2 | 113.5 |
| Haversine Formula (independent check) | 153.3 | 113.5 |
| Google Maps Driving Distance (original example) | 151.86 | N/A |
Note that the great-circle distance (the shortest path along the Earth's curved surface) is close to, but not identical to, driving distance — roads follow terrain and infrastructure rather than the true geodesic path. The Google Maps row only applies to the original State College → Philadelphia case study and won't change when you try other locations.