rotate method

GeoCoordinate2D rotate(
  1. GeoAngle angle, [
  2. GeoCoordinate2D? pivot
])

Rotate coordinate by specified angle using pivot as center of rotation.

Implementation

GeoCoordinate2D rotate(GeoAngle angle, [GeoCoordinate2D? pivot]) {
  if (angle.degree == 0) return GeoCoordinate2D(x, y);
  // rotation matrix
  double mr1c1, mr1c2, mr2c1, mr2c2;
  mr1c1 = cos(angle.radian);
  mr1c2 = -sin(angle.radian);
  mr2c1 = sin(angle.radian);
  mr2c2 = cos(angle.radian);

  // adjust coordinate if we have a pivot point
  double cx, cy;
  cx = pivot != null ? x - pivot.x : x;
  cy = pivot != null ? y - pivot.y : y;

  // calculate new coordinates by multiplying matrix with adjusted coordinates
  double newx = cx * mr1c1 + cy * mr1c2;
  double newy = cx * mr2c1 + cy * mr2c2;

  if (pivot != null) {
    newx = newx + pivot.x;
    newy = newy + pivot.y;
  }
  // return new coordinate
  return GeoCoordinate2D(newx, newy);
}