rotateBy method

Point rotateBy(
  1. Angle angle, [
  2. Point? origin
])

Rotates the point around a given origin by a specific `angle (in radians).

The rotation is performed counterclockwise. If no origin is provided, the rotation is performed around the origin (0, 0).

Example:

var point = Point(2, 0);
var origin = Point(1, 0);
Angle angle = Angle.radians(math.pi / 2)
var rotated = point.rotateBy(angle, origin);
print(rotated); // Output: Point(1, 1)

Returns a new Point instance representing the rotated point.

Implementation

Point rotateBy(Angle angle, [Point? origin]) {
  origin = origin ?? Point.origin(false);

  var cosAngle = cos(angle.rad);
  var sinAngle = sin(angle.rad);

  var translatedX = x - origin.x;
  var translatedY = y - origin.y;

  var rotatedX = translatedX * cosAngle - translatedY * sinAngle;
  var rotatedY = translatedX * sinAngle + translatedY * cosAngle;

  x = rotatedX + origin.x;
  y = rotatedY + origin.y;

  return z != null ? Point(x, y, z) : Point(x, y);
}