rotate method
- double deg
override
Rotate the shape by deg degrees
Implementation
@override
Rectangle rotate(double deg) {
// Rotation breaks axis-alignment, so rotate all corners
// and return the bounding rectangle of the result
final c = center;
final rotated = vertices.map((v) {
final dx = v.x - c.x;
final dy = v.y - c.y;
final rad = deg * math.pi / 180;
final nx = dx * math.cos(rad) - dy * math.sin(rad) + c.x;
final ny = dx * math.sin(rad) + dy * math.cos(rad) + c.y;
return Point(nx, ny);
}).toList();
final minX = rotated.map((p) => p.x).reduce(math.min);
final minY = rotated.map((p) => p.y).reduce(math.min);
final maxX = rotated.map((p) => p.x).reduce(math.max);
final maxY = rotated.map((p) => p.y).reduce(math.max);
return Rectangle(
x: minX,
y: minY,
width: maxX - minX,
height: maxY - minY,
);
}