# Why my flight-map arc to Los Angeles got cut in half: great circles across the antimeridian

> I built a small tool that turns pasted flights into a poster. In the first test the Tokyo to Los Angeles arc was a short stub: the dot was at the left of the map and the line ran off to the right. Longitudes -118 and 242 are the same place.

- Date: 2026-11-29
- Topics: Data
- Chinese original: https://kbs0830.com/blog/great-circle-flight-poster-antimeridian

The site has a new small tool: the [flight record poster](/tools/flight-poster) (in Chinese). Paste the routes you have flown (three-letter airport codes, one route per line), and the browser draws a route map you can download as PNG or SVG. The routes are not uploaded.

The approach is not hard: airport coordinates come from the OurAirports data used by the [airport distance tool](/tools/airport-distance) (in Chinese), a great-circle arc is drawn between two airports, and everything sits on an equirectangular projection. The hard part is a trap I hit in my very first test.

## What the first version looked like

The default example is `TPE-NRT-LAX` (Taipei → Tokyo → Los Angeles). The drawing showed:

- Tokyo, Taipei, Fukuoka and Kaohsiung crowded at the right of the map, which is fine
- the Los Angeles dot at the **far left** of the map
- the arc from Tokyo to Los Angeles drawn only as a short stretch going up and to the right from Tokyo, and then out of the frame

Neither the dots nor the lines were computed wrong; the error is that they used different "longitudes".

## Longitude -118 and 242 are the same place

Los Angeles is at longitude -118.4 and Tokyo at 140.4. Put them on a map from -180 to 180, and Los Angeles is at the far left and Tokyo toward the right, 258 degrees apart, with all of Eurasia, Africa and the Americas between them.

But planes do not fly like that. Tokyo to Los Angeles goes east across the Pacific, with longitude increasing from 140 through 180 to -180 and on to -118. **If the arc's longitude is continuous**, it goes 140 → 180 → 242 (242 is -118). My arc function was written correctly and kept longitude continuous, so the arc's end is at 242; but the airport dots used the original -118. One was drawn at 242, the other at -118, and the map framed only one side, so the dots and lines ended up in different places.

## The fix: first move all airports to the same side

Before drawing anything, "unwrap" all airports' longitudes to the same side:

1. Sort all the longitudes
2. Find the **largest gap** (where adjacent longitudes differ the most, treated as a circle, with the last one connecting back to the first plus 360). That gap is the part you do not need to draw, such as the Pacific
3. The first longitude after the gap is the westmost; everything smaller than it gets +360

TPE, NRT and LAX have longitudes 121, 140 and -118. Sorted: -118, 121, 140, and the largest gap is between -118 and 121 (239 degrees). So 121 is the westmost, and LAX becomes -118 + 360 = 242. After that: 121, 140, 242, all on one side with no big hole between them, and the map range only needs to frame 121 to 242.

The arc does not need changing because it was already continuous; the dots and the framing range both use the unwrapped longitudes, and the two sides line up.

```ts
export function unwrapAirports(airports: PosterAirport[]): PosterAirport[] {
  if (airports.length < 2) return airports;
  const lons = airports.map((a) => a.lon).toSorted((x, y) => x - y);
  let gap = -1;
  let westIdx = 0;
  for (let i = 0; i < lons.length; i++) {
    const next = i + 1 < lons.length ? lons[i + 1] : lons[0] + 360;
    if (next - lons[i] > gap) {
      gap = next - lons[i];
      westIdx = (i + 1) % lons.length;
    }
  }
  const west = lons[westIdx];
  return airports.map((a) => ({ ...a, lon: a.lon < west ? a.lon + 360 : a.lon }));
}
```

## How the great-circle arc is computed

"The shortest path along the Earth's surface" between two airports is a great circle. Convert the two points to 3D unit vectors and interpolate linearly along the sphere (slerp):

```
point(t) = sin((1-t)·Ω)/sin(Ω) · v1 + sin(t·Ω)/sin(Ω) · v2      Ω = angle between the two vectors
```

Cut t from 0 to 1 into 48 steps, convert each point back to latitude and longitude, and connect them as a polyline. When two points almost coincide, `sin(Ω)` is close to 0 and would divide by zero, so that case just returns the start point. When converting back to longitude, each point is compared with the previous one: if the difference exceeds 180, ±360 is applied to keep longitude continuous.

## Limits of this approach

- It only "makes common trans-Pacific routes draw correctly". If routes go east across the Pacific and also west across the Atlantic (round the world), the airports are spread all around and there is no obvious large gap, so the map gets very wide
- An equirectangular projection stretches things at high latitudes; the poster scales longitude by "the cos of the middle latitude", which is not a true map projection
- The arc looks "curved" because of the projection; in reality it is the shortest path on the Earth's surface

## Tests

The pure functions all have unit tests: `unwrapAirports` must confirm Los Angeles becomes 241.6, airports already on one side do not move, and the largest span after unwrapping is under 180; the arc test confirms that adjacent points never differ by more than 30 degrees of longitude (a jump means ±180 was not handled), and that no NaN appears when two airports coincide. The browser test pastes routes, confirms the poster appears, and that both PNG and SVG download.

---

Author: PINGWEI LI (李秉威, Li Ping-Wei)
Original page: https://kbs0830.com/en/blog/great-circle-flight-poster-antimeridian
