Skip to main content

Function: unwrapAlong()

unwrapAlong(valueAt, options): number

Defined in: unwrap.ts:60

Unwrap a value that wraps around, at the end of a path, given its unwrapped value at the start.

valueAt(t) gives the value at t along the path, from 0 to 1, in any copy. The path is walked in steps, and each step is moved to the copy nearest the step before. Steps are halved until each moves less than a quarter period, and the midpoint is always checked, so two ends a whole period apart are found: like the two edges of a 360°-wide image, which come back from a projection at the same longitude.

The result is right when the value changes smoothly along the path, as a longitude does along a straight line across an image. A path that winds around a pole, or a value that jumps most of a period between two steps the walk accepts, has no single answer and can give the wrong copy.

Parameters​

valueAt​

(t) => number

The value at t along the path, in any copy.

options​

period​

number

The period of the value, such as 360 for a longitude.

start​

number

The unwrapped value at t = 0.

Returns​

number

The unwrapped value at t = 1.

Example​

A path that runs east from longitude 170° to 190°, but whose values come back wrapped into [-180°, 180°], as a projection returns them: past 180° they jump to the far side of the world, so the end reads -170°. Unwrapped, the end is 190°, next to where the path started.

const lngAt = (t: number) => {
const lng = 170 + 20 * t;
return lng > 180 ? lng - 360 : lng;
};

lngAt(1); // -170
unwrapAlong(lngAt, { start: 170, period: 360 }); // 190