getBoundsTransformed too big after rotation?


#1

I’m running into a strange problem getting the bounds of a rotated rectangle and I hope the smarter people than me here can point me in the right direction.

I have a set of rectangles that I want to fill each with a checkerboard. I’m reading in the four cornerpoints of each rectangle from xml and add them to a Path. I can then get the bounds of the path to figure out how big my checkerboard should be, draw it into a graphic context and then end with drawing the path itself as an outline.

All is well until I have to draw a rotated path. The xml I’m using gives me the rotated coordinates of the four corner points and their rotation in radians. I can get the checkerboard to line up correctly by rotating the graphic context around the center of the path with the parsed rotation value, drawing the checkerboard, rotating the context back and drawing the outline. So far so good, but my problem is getting the bounds of the rotated path.

As I said, the points are stored post-rotation, so I figured I could get the original values by applying a rotation on the path, and then getting its bounds. I found the function getBoundsTransformed that seems to do what I need.

//rotation is a float for the angle in radians and center is a Point<float> for the path's centerpoint
Rectangle<float> bounds = path.getBoundsTransformed( AffineTransform::rotation( rotation, center.x, center.y )); 

Strangely enough, when using this function on a square rectangle rotated around a 45 degree angle, the bounds that are returned are twice as big as what they actually are. I would expect a little error for rotating the wrong way or something, but twice as big seems weird. Different aspect ratios and different rotation values give different results, but the returned bounds are always bigger than they need to be.

Can anybody spot what I’m doing wrong? Am I completely misunderstanding what getBoundsTransformed is supposed to do? Or maybe there is a less round about way to go about getting the bounds of the rotated rectangle?

Appreciate anyone that read this far, and happy to elaborate or go into details if I didn’t manage to make myself clear enough.


#2


For the visually inclined, this image shows the paths rotated through 0 to -90 degrees. The bounds returned from getBoundsTransformed are used to draw the checkerboard and circles.

At 0 and -90 degrees, everything is fine, but at the rotation values in between, the bounds are too big.


#3

Twice or roughly twice? I would expect them to be 1.41 times as big. That is because if you rotate a square (for simplicity) by 45 degrees, the enclosing rectangle needs to be as long as the diagonals of the original square, when aligned with the dimensions. So the area covered will be twice as big, since each diagonal is sqrt (2) times the original length.

HTH


#4

So instead of the path being transformed and getting the bounds of that, the bounds of the path are transformed and the new bounds are returned. That makes sense when you think about it.

Thanks for the headspace!