translatePosition<C extends num> function

TransformPosition translatePosition<C extends num>({
  1. C? dx,
  2. C? dy,
  3. C? dz,
  4. C? dm,
})

Returns a function to translate positions by delta values of each axis.

Set optional dx, dy, dz and dm values for translating on a corresponding axis.

If a point to be translated do not have an axis even if a translation delta for that axis is given, then such delta is ignored.

Implementation

TransformPosition translatePosition<C extends num>({
  C? dx,
  C? dy,
  C? dz,
  C? dm,
}) =>
    <T extends Position>(T source) {
      final dim = source.coordinateDimension;
      if (dim == 2) {
        // point is (X, Y)
        return source.copyWith(
          x: dx != null ? source.x + dx : null,
          y: dy != null ? source.y + dy : null,
        ) as T;
      } else {
        // point could be (X, Y, Z), (X, Y, M) or (X, Y, Z, M)
        return source.copyWith(
          x: dx != null ? source.x + dx : null,
          y: dy != null ? source.y + dy : null,
          z: dz != null && source.is3D ? source.z + dz : null,
          m: dm != null && source.isMeasured ? source.m + dm : null,
        ) as T;
      }
    };