rotate method

  1. @override
LineSegment rotate(
  1. double angle, [
  2. Point<num>? origin
])
override

Rotates the shape by the given angle (in radians) around the origin. If origin is omitted, rotates around the shape's centroid.

Implementation

@override
LineSegment rotate(double angle, [Point<num>? origin]) {
  final rotOrigin = origin ?? centroid;
  final ox = rotOrigin.x.toDouble();
  final oy = rotOrigin.y.toDouble();
  final cosA = math.cos(angle);
  final sinA = math.sin(angle);

  Point<double> rotatePoint(Point<num> p) {
    final px = p.x.toDouble();
    final py = p.y.toDouble();
    final nx = cosA * (px - ox) - sinA * (py - oy) + ox;
    final ny = sinA * (px - ox) + cosA * (py - oy) + oy;
    return Point(x: nx, y: ny);
  }

  return LineSegment(p1: rotatePoint(p1), p2: rotatePoint(p2));
}