开发者

Smallest possible bounding box for a rotated image

Suppose I have some arbitrary input image with width w1 and height h1. I want to rotate this image all the way around 360 degrees back to 开发者_如何学Cthe starting position. However, if the image is of anything except a circle, then the edges of the image will be clipped if the canvas it is drawn onto remains at size w1 by h1.

Smallest possible bounding box for a rotated image

What I need then is to determine the canvas size (width w2 and height h2) that can be used for all rotated versions of the input image. I know that w2 == h2, and thus the desired canvas size is a square, because it is obvious that we are rotating something about a center point, and the final image (after 360 rotations) is essentially a circle.

The other thing to consider is that objects such as squares will have corners that will stick out, so just using the max value of width or height for both dimensions also does not work.

One solution I have come up with is to create the canvas larger than I need (for example by setting w2 and h2 to max(w1, h1) * 2, rotating everything, and then trimming all the transparent pixels. This is not very efficient and I would much rather be able to calculate the tightest bounding box upfront.


This is a geometry question. You essentially want to find the diameter (d) of a circle that would inscribe your original canvas and then w2 = h2 = d

The diameter of such a circle would be √(w1^2+h1^2)

So w2 = h2 = √(w1^2+h1^2)

Also, to avoid clipping, you might want to take the ceiling of that result rather than rounding.


If the image being rotated in a square, you'd have to make the canvas height and width the same length as the hypotenuse.

w = h = sqrt(h^2 + w^2)

(I do not know actionscript)

However, if the image you have is not in a square, you'll essentially have to find the point farthest away from the center...

PS: It's late and I'm rambling, so I'm sorry if this might be wrong.


Your canvas need to be a square.

If you are going to rotate a body like the green figure around any point (in this example Point A), the side of the square is the double of the distance to the most distant point to A in the body.

Smallest possible bounding box for a rotated image

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜