Create rotated rect in dart

76 Views Asked by At

I have an interesting problem. I am drawing a rectangle on a canvas in flutter. I use the Rect object to define what the rectangle should look like. If I use canvas rotation functions I get the desired result. If I try and manually create a rotated rectangle though, it does not function correctly. Here is my function to rotate a rectangle

  static Rect rotateRect(Rect rect, Offset center, double angle) {
final Offset leftTop = rotateOffset(rect.topLeft, center, angle);
final Offset bottomLeft = rotateOffset(rect.bottomLeft, center, angle);

final Offset rightTop = rotateOffset(rect.topRight, center, angle);
final Offset bottomRight = rotateOffset(rect.bottomRight, center, angle);

return Rect.fromPoints(leftTop, bottomRight);}

Here is the result ( The green is rotation using the canvas and is correct, the purple is using my rotation function to try and construct a rotated rectangle ):

Rotation problem

As you can see, since only two points are used to create the rectangle, only the top left and bottom right points are correct. Everything I try does not seem to allow me to get a rectangle that is rotated.

please note I am only looking for solutions to build a rectangle inside the Rect object. I am not looking for other strategies to achieve the same effect on a canvas.

0

There are 0 best solutions below