旋转图像的最小可能边界框

发布于 2024-10-09 23:44:17 字数 567 浏览 2 评论 0原文

假设我有一些宽度为 w1 和高度 h1 的任意输入图像。我想将此图像旋转 360 度回到起始位置。但是,如果图像不是圆形,则如果绘制图像的画布保持 w1h1 大小,则图像的边缘将被剪裁。

Figure

然后我需要确定画布大小(宽度 w2 和高度 h2),可用于输入图像的所有旋转版本。我知道 w2 == h2,因此所需的画布大小是一个正方形,因为很明显我们正在围绕中心点旋转某些东西,并且最终图像(旋转 360 度后)本质上是一个圆。

另一件需要考虑的事情是,正方形等对象会有突出的角,因此仅使用两个尺寸的宽度或高度的最大值也是行不通的。

我想出的一种解决方案是创建比我需要的更大的画布(例如,将 w2h2 设置为 max(w1, h1) * 2,旋转所有内容,然后修剪所有透明像素,这不是很有效,我宁愿能够预先计算最紧密的边界框。

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 the 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.

Figure

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北城半夏 2024-10-16 23:44:17

这是一道几何题。您本质上是想找到一个圆的直径 (d),该圆将刻在您的原始画布上,然后 w2 = h2 = d

这样一个圆的直径将是 √(w1^2+h1^2)

所以 w2 = h2 = √(w1^2+h1^2)

另外,为了避免削波,您可能需要取结果的上限而不是四舍五入。

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.

許願樹丅啲祈禱 2024-10-16 23:44:17

如果图像以正方形旋转,则必须使画布的高度和宽度与斜边的长度相同。

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

(我不知道动作脚本)

但是,如果您拥有的图像不是正方形,那么您基本上必须找到距离中心最远的点。 PS

:已经晚了,我在胡言乱语,所以如果这可能是错误的,我很抱歉。

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.

開玄 2024-10-16 23:44:17

你的画布必须是正方形的。

如果要围绕任意点(在本例中为点 A)旋转像绿色图形这样的物体,则正方形的边长是物体中到 A 最远点的距离的两倍。

替代文本

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.

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文