export function quantize()

in src/lib/components/molecules/canvas-map/lib/util/simplify.js [318:397]


export function quantize(
  flatCoordinates,
  offset,
  end,
  stride,
  tolerance,
  simplifiedFlatCoordinates,
  simplifiedOffset,
) {
  // do nothing if the line is empty
  if (offset == end) {
    return simplifiedOffset
  }
  // snap the first coordinate (P1)
  let x1 = snap(flatCoordinates[offset], tolerance)
  let y1 = snap(flatCoordinates[offset + 1], tolerance)
  offset += stride
  // add the first coordinate to the output
  simplifiedFlatCoordinates[simplifiedOffset++] = x1
  simplifiedFlatCoordinates[simplifiedOffset++] = y1
  // find the next coordinate that does not snap to the same value as the first
  // coordinate (P2)
  let x2, y2
  do {
    x2 = snap(flatCoordinates[offset], tolerance)
    y2 = snap(flatCoordinates[offset + 1], tolerance)
    offset += stride
    if (offset == end) {
      // all coordinates snap to the same value, the line collapses to a point
      // push the last snapped value anyway to ensure that the output contains
      // at least two points
      // FIXME should we really return at least two points anyway?
      simplifiedFlatCoordinates[simplifiedOffset++] = x2
      simplifiedFlatCoordinates[simplifiedOffset++] = y2
      return simplifiedOffset
    }
  } while (x2 == x1 && y2 == y1)
  while (offset < end) {
    // snap the next coordinate (P3)
    const x3 = snap(flatCoordinates[offset], tolerance)
    const y3 = snap(flatCoordinates[offset + 1], tolerance)
    offset += stride
    // skip P3 if it is equal to P2
    if (x3 == x2 && y3 == y2) {
      continue
    }
    // calculate the delta between P1 and P2
    const dx1 = x2 - x1
    const dy1 = y2 - y1
    // calculate the delta between P3 and P1
    const dx2 = x3 - x1
    const dy2 = y3 - y1
    // if P1, P2, and P3 are colinear and P3 is further from P1 than P2 is from
    // P1 in the same direction then P2 is on the straight line between P1 and
    // P3
    if (
      dx1 * dy2 == dy1 * dx2 &&
      ((dx1 < 0 && dx2 < dx1) || dx1 == dx2 || (dx1 > 0 && dx2 > dx1)) &&
      ((dy1 < 0 && dy2 < dy1) || dy1 == dy2 || (dy1 > 0 && dy2 > dy1))
    ) {
      // discard P2 and set P2 = P3
      x2 = x3
      y2 = y3
      continue
    }
    // either P1, P2, and P3 are not colinear, or they are colinear but P3 is
    // between P3 and P1 or on the opposite half of the line to P2.  add P2,
    // and continue with P1 = P2 and P2 = P3
    simplifiedFlatCoordinates[simplifiedOffset++] = x2
    simplifiedFlatCoordinates[simplifiedOffset++] = y2
    x1 = x2
    y1 = y2
    x2 = x3
    y2 = y3
  }
  // add the last point (P2)
  simplifiedFlatCoordinates[simplifiedOffset++] = x2
  simplifiedFlatCoordinates[simplifiedOffset++] = y2
  return simplifiedOffset
}