如何旋转的二维阵列,以任意的程度? [英] How to rotate a two-dimensional array to an arbitrary degree?

查看:96
本文介绍了如何旋转的二维阵列,以任意的程度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个布尔[] [],我想通过37度旋转。我知道的转型并不总是完美的,那也没关系。我已经在这里上类似我的问题准备好充足的答案,但唯一的解决方案,我发现只有解决问题90度的增量。

Say I have a bool[][], and I want to rotate it by 37 degrees. I am aware that the transformation wouldn't always be perfect, and that's okay. I've ready plenty of answers on here similar to my question, but the only solutions I've found only solve the problem for 90 degree increments.

推荐答案

最好的办法是遍历所有的目标位置,并为每个读取正确的源位置。如果你试图周围(即循环源和目的地写)的其他方式,你将最终的差距。

The best way is to loop over the destination locations and for each of them read the correct source location. If you try the other way around (i.e. looping on source and writing on destination) you will end up with gaps.

旋转公式很简单...

The rotation formula is simple...

source_x = dest_x * c + dest_y * s + x0
source_y = dest_x * -s + dest_y * c + y0

其中, C 是角,取值的余弦值是角度的正弦和 X0,Y0 用于正确转换旋转图像。在psedudo code

where c is the cosine of the angle, s is the sine of the angle and x0, y0 are used to correctly translate the rotated image. In psedudocode

for y = 0, 1, ... dest_height
    for x = 0, 1, ... dest_width
        src_x = c*x + s*y + x0
        src_y = -s*x + c*y + y0
        copy from source[src_y][src_x] to dest[y][x]

X0,Y0 可以计算,以使源极中心将在目标中心最终通过

x0, y0 can be computed so that the source center will end up in the destination center by

 x0 = src_width/2 - c*dest_width/2 - s*dest_height/2
 y0 = src_height/2 - c*dest_height/2 + s*dest_width/2

如果不是仅仅使用 C = COS(角度) S =罪(角)您同时调整他们的一个因素 K 生成的图像将被旋转和缩放围绕中心

If instead of just using c = cos(angle) and s = sin(angle) you scale both of them with a factor k the resulting image will be rotated and zoomed around the center.

还要注意的是公式中的 X 双线性;这意味着你可以使用完整的配方为完整的价值为行的第一像素,然后就做从src_x + = C src_y - = S 同一行中的每个元素,因为这是从移动当x 会发生什么X + 1

Note also that the formulas are bilinear in x and y; this means that you can use the full formula for the complete value for the first pixel of a row and then just do src_x += c and src_y -= s for each element of the same row because that is what happens when moving from x to x+1.

还请注意,根据源和目标的大小也可能是所计算的源元素是不可用,因为出图像的。在这种情况下,有几种通常使用的选项

Note also that depending on source and destination size it may be that the computed source element is not available because out of the image. In this case there are several normally used options


  1. 写一个固定值(例如

  2. 请不要写目标单元格

  3. 阅读前做了夹紧限制两个坐标为最大允许值

  4. 请一个平铺使用模运算符正常化坐标

这篇关于如何旋转的二维阵列,以任意的程度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆