旋转图像的最小可能边界框
假设我有一些宽度为 w1 和高度 h1 的任意输入图像。我想将此图像旋转 360 度回到起始位置。但是,如果图像不是圆形,则如果绘制图像的画布保持 w1 乘 h1 大小,则图像的边缘将被剪裁。
然后我需要确定画布大小(宽度 w2 和高度 h2),可用于输入图像的所有旋转版本。我知道 w2 == h2,因此所需的画布大小是一个正方形,因为很明显我们正在围绕中心点旋转某些东西,并且最终图像(旋转 360 度后)本质上是一个圆。
另一件需要考虑的事情是,正方形等对象会有突出的角,因此仅使用两个尺寸的宽度或高度的最大值也是行不通的。
我想出的一种解决方案是创建比我需要的更大的画布(例如,将 w2 和 h2 设置为 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一道几何题。您本质上是想找到一个圆的直径 (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.
如果图像以正方形旋转,则必须使画布的高度和宽度与斜边的长度相同。
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.
你的画布必须是正方形的。
如果要围绕任意点(在本例中为点 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.