Skip to content

Documentation / @interstellar-tools/maps / joinSegments

Function: joinSegments() ​

ts
function joinSegments(
  segments: readonly (readonly [GridPositionInterface, GridPositionInterface])[]
): GridPositionInterface[][];

Defined in: terrain/join-segments.ts:37

Join line segments into lines, such as a contour's from contours: each line runs through the segments that meet end to end.

Segments meet where their ends are the same point exactly, as neighbouring squares' are in contours.

Units

  • Inputs and output: positions in grid coordinates, as given.
  • Output: lines of two points or more. A closed line, such as a contour around a hill, ends on a copy of its first point.

Notes

  • An open line, one that stops at the grid's edge or at missing cells, starts at one of its ends, so it comes out whole rather than in two parts.
  • Every segment is in exactly one line, except a segment of length 0, which is left out. contours gives those only where a cell's value is exactly a contour's level.
  • Where more than two segments meet at a point, which takes a cell exactly at the level, the lines are split there: each is still a run of segments meeting end to end.
  • The lines' direction, and their order, are not defined: don't rely on the higher ground being on one side.

Parameters ​

ParameterTypeDescription
segmentsreadonly (readonly [GridPositionInterface, GridPositionInterface])[]The segments, each from one point to another.

Returns ​

GridPositionInterface[][]

The lines, each a list of points.

Example ​

ts
// The 5 m contour around a single peak: one closed diamond
const [contour] = contours(grid, [5]);
const [line] = joinSegments(contour.segments);
line.length; // 5: 4 corners, and the first again

See ​

contours