← Math
Math

Angles, trigonometry, and coordinate geometry

Why does the courier icon spin the wrong way?

Carry coordinate and angle conventions through every map-to-screen calculation.

Outcome

Compute a compass bearing with atan2, place and rotate an icon across two coordinate frames, and turn it the short way through north.

Terminology (5)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Radian
The angle whose arc equals the radius; a full turn is 2π radians, or 360°.
Bearing
A direction in degrees clockwise from north, from 0 up to but not including 360.
atan2(y, x)
The angle of the point (x, y) from the positive x axis, using both signs to pick the quadrant.
Coordinate frame
An origin, axis directions and units. The map grid has north up; the screen has y growing down.
Sine and cosine
For a bearing θ, a 1-metre move goes sin θ metres east and cos θ metres north.

Why this matters

Maps, games, charts and drawing tools all turn positions into directions and back. Each involves a convention: where zero is, which way angles grow, which way y points. Mixing two conventions gives numbers that look reasonable and pictures that are wrong.

This lesson assumes signed coordinates, Pythagoras and remainders.

Coordinates and angle conventions

A coordinate frame fixes an origin and the directions in which coordinates increase. Subtracting two positions gives a displacement in that frame. An angle describes its direction only after choosing a zero direction and a positive sense of rotation.

Radians measure a full turn as 2π; degrees measure it as 360°. Compass bearings and screen rotations use different conventions, so converting a position and converting a heading must agree about axes and units. The worked map uses a flat city grid; geographic distances and bearings need a different model at larger scales.

01Bearing from two fixes

Bearing from two fixes

Your food-delivery app shows the courier moving on a map, with an arrow icon that turns to face the direction of travel. Customers report that the arrow sometimes points backwards, is drawn mirrored after a map refactor, and spins almost a full circle whenever the courier turns through north.

The app converts each GPS fix to metres east and north on a flat city grid. A courier who moves 30 m east and 40 m north has travelled 50 m. A compass bearing measures from north, clockwise, so this move is atan2(30, 40) ≈ 0.644 radians, or 36.9°: more north than east.

A full turn is 2π radians or 360°, so multiply radians by 180/π for degrees. Maths textbooks measure from east, anticlockwise: atan2(40, 30) = 53.1°. Both are correct in their own convention. The map needs bearings, and putting east first and north second in atan2 gives one directly.

02atan2 keeps the quadrant

atan2 keeps the quadrant

The next move is 20 m west and 25 m south. The quotient −20 / −25 is 0.8, the same as for 20 m east and 25 m north, so atan of the quotient says 38.7°, north-east. atan2 sees both signs and returns −141.3°; adding 360 gives the bearing 218.7°, south-west.

Math.atan2(y, x) returns radians from −π to π; multiply by 180 / Math.PI for degrees. Math.atan(y / x) loses the quadrant.

03Map frame to screen frame

Map frame to screen frame

On the map grid north grows upward. On a screen, y grows downward from the top-left corner. At 2 pixels per metre, with the view’s corner at (1000, 2000) on the grid, the fix (1070, 1990) is drawn at x = 70 × 2 = 140 and y = (2000 − 1990) × 2 = 20. Forget the flip and this point lands at y = −20, above the view; the vertical component of each rotation is mirrored.

A canvas rotates clockwise for positive angles because its y points down. For the earlier move of 30 m east and 40 m north, the screen displacement is (30, −40). Its canvas angle is atan2(−40, 30) ≈ −53.1°: up and to the right.

The courier heads due north. What canvas rotation does the arrow get, if the artwork points right?

04Turning through north

Turning through north

Predict both lines.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// The icon shows 350° and the next move heads 10°. Which way does it turn?
const from = 350;
const to = 10;

console.log((to - from).toFixed(1)); // subtract
console.log((((to - from + 540) % 360) - 180).toFixed(1)); // the short way round

Subtracting headings ignores that 360° and 0° are the same direction. Adding 540, taking the remainder by 360 and subtracting 180 maps differences between normalized headings into [−180, 180), the short way round. At exactly half a turn both directions are equally short; the lab avoids that ambiguous case. With factor t = 0.5, a +20° turn adds only +10°, leaving a displayed heading of 0°. The lab uses a positive jitter threshold, 0 ≤ t ≤ 1 and at least two fixes. Moves shorter than the threshold keep both the heading and the last accepted fix: several small steps can eventually accumulate into an accepted move. This gate reduces noise; small moves can also be real slow travel.

05What the flat grid assumes

What the flat grid assumes

06Fix the courier icon

Fix the courier icon

Repair the courier marker’s compass convention and map-to-screen conversion, then keep its displayed heading continuous through north. The jitter gate must remember the last accepted position so slow travel still accumulates.

A live-tracking map

Compute bearings, convert to the screen, then turn the icon the short way.

  1. easy · M-ANGLES-V4-1 Bearing of a move
  2. medium · M-ANGLES-V4-2 Place and rotate the icon
  3. hard · M-ANGLES-V4-3 Smooth the heading
In the frontend The courier marker in Next.js and Svelte Optional

A CSS transition animates the number in rotate(), so going from 350deg to 10deg spins 340°. Keeping an unwrapped angle and adding only the short turn fixes it. These UI snippets use north-pointing artwork, so a compass bearing is its CSS rotation. The canvas example above assumes right-pointing artwork.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// The courier marker. A CSS transition from rotate(350deg) to rotate(10deg) spins 340° the long way,
// so keep an unwrapped angle and add only the short turn each time.
import { useEffect, useState } from "react";

export function CourierMarker({
  x,
  y,
  bearing
}: {
  x: number;
  y: number;
  bearing: number;
}) {
  const [angle, setAngle] = useState(bearing);

  useEffect(() => {
    setAngle(
      (previous) => previous + ((((bearing - previous) % 360) + 540) % 360) - 180
    );
  }, [bearing]);

  return (
    <img
      src="/courier.svg"
      alt=""
      style={{
        left: x,
        top: y,
        transform: `rotate(${angle}deg)`,
        transition: "transform 0.4s"
      }}
    />
  );
}

Check this idea later
A move goes 10 m west and 10 m north. What is its bearing?
What is 90° in radians?
Sources

Values above were run in Node 22, Go 1.27, Rust 1.99 and Apple clang 17. The delivery app, its grid and the fixes are a worked model.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.