pointFromTo2D static method

GeoCoordinate2D pointFromTo2D(
  1. GeoCoordinate2D from,
  2. GeoCoordinate2D to,
  3. double length
)

Calculate the point length distance from point from, moving towards point to in a 2-dimensional plane.

Implementation

static GeoCoordinate2D pointFromTo2D(
    GeoCoordinate2D from, GeoCoordinate2D to, double length) {
  if (from == to) return from; // from and to are the same point.
  double deltay = (to.y - from.y);
  double deltax = (to.x - from.x);
  if (deltax == 0) {
    // vertical
    return GeoCoordinate2D(from.x, from.y + deltay.sign * length);
  } else if (deltay == 0) {
    // horizontal
    return GeoCoordinate2D(from.x + deltax.sign * length, from.y);
  } else {
    double deltah = sqrt(pow(deltax, 2) + pow(deltay, 2));
    double factor = length / deltah;
    return GeoCoordinate2D(
        from.x + factor * deltax, from.y + factor * deltay);
  }
}