如何将2D阵列以小于90°的角度旋转到最佳近似值? [英] How can I rotate a 2d array by LESS than 90°, to the best approximation?
问题描述
说我有一个旋转0度存储的数组:
Say I have an array that is stored in 0° rotation:
0 0 1 0 0
0 0 1 0 0
1 1 1 0 0
0 0 0 0 0
0 0 0 0 0
如果我通过(例如30°作为参数),我希望它返回的近似值是这样的:
And I want it returned in a good approximation if I pass, for example 30° as parameter, it would be something like:
0 0 0 1 0
1 1 0 1 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
45°将是
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
I am aware of the solutions posted for 90° rotations. But I don't think that will help me here?
我没有任何示例代码,因为我目前甚至不知道从哪里开始寻找。如果有任何我可以用谷歌搜索的关键字指出可以适应的某些公式,那也很棒。
I don't have any examplecode because I currently wouldn't even know where to start looking. If there are any keywords I can google that point me in the directions of some formula I can adapt for this, that would also be great.
Spectre在C#中的代码解决方案:
Spectre's code solution in C#:
class Rotation
{
public Rotation() {
A = new int[xs,ys]{
{0,0,0,9,0,0,0},
{0,0,0,9,0,0,0},
{0,0,0,9,0,0,0},
{9,9,9,9,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
};
B = new int[xs, ys];
deg = (float)(Math.PI / 180.0);
}
public const int xs = 7; // matrix size
public const int ys = 7;
const int x0 = 3; // rotation center cell
const int y0 = 3;
readonly float deg;
public int[,] A;
public int[,] B;
//---------------------------------------------------------------------------
public void rotcv(float ang) {
rotcw(Rotation.x0, Rotation.y0, ang);
}
private void rotcw(int x0, int y0, float ang) // rotate A -> B by angle ang around (x0,y0) CW if ang>0
{
int x, y, ix0, iy0, ix1, iy1, q;
double xx, yy, fx, fy, c, s;
// circle kernel
c = Math.Cos(-ang); s = Math.Sin(-ang);
// rotate
for (y = 0; y < ys; y++)
for (x = 0; x < xs; x++)
{
// offset so (0,0) is center of rotation
xx = x - x0;
yy = y - y0;
// rotate (fx,fy) by ang
fx = ((xx * c) - (yy * s));
fy = ((xx * s) + (yy * c));
// offset back and convert to ints and weights
fx += x0; ix0 = (int)Math.Floor(fx); fx -= ix0; ix1 = ix0 + 1; if (ix1 >= xs) ix1 = ix0;
fy += y0; iy0 = (int)Math.Floor (fy); fy -= iy0; iy1 = iy0 + 1; if (iy1 >= ys) iy1 = iy0;
// bilinear interpolation A[fx][fy] -> B[x][y]
if ((ix0 >= 0) && (ix0 < xs) && (iy0 >= 0) && (iy0 < ys))
{
xx = (A[ix0,iy0]) + ((A[ix1,iy0] - A[ix0,iy0]) * fx);
yy = (A[ix0,iy0]) + ((A[ix1,iy0] - A[ix0,iy0]) * fx);
xx = xx + ((yy - xx) * fy); q =(int) xx;
}
else q = 0;
B[x,y] = q;
}
}
}
测试:
static void Main(string[] args)
{
Rotation rot = new Rotation();
for (int x = 0; x < Rotation.xs; x++) {
for (int y = 0; y < Rotation.xs; y++) {
Console.Write(rot.A[x,y] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
float rotAngle = 0;
while (true)
{
rotAngle += (float)(Math.PI/180f)*90;
rot.rotcv(rotAngle);
for (int x = 0; x < Rotation.xs; x++)
{
for (int y = 0; y < Rotation.xs; y++)
{
Console.Write(rot.B[x, y] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
Console.ReadLine();
}
}
推荐答案
在这里按承诺完成。第一个 C ++ 代码:
Ok here it is as promised. First C++ code:
//---------------------------------------------------------------------------
#include <math.h>
//---------------------------------------------------------------------------
const int xs=7; // matrix size
const int ys=7;
const int x0=3; // rotation center cell
const int y0=3;
const float deg=M_PI/180.0;
int A[xs][ys]=
{
{0,0,0,9,0,0,0},
{0,0,0,9,0,0,0},
{0,0,0,9,0,0,0},
{9,9,9,9,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
};
int B[xs][ys];
//---------------------------------------------------------------------------
void rotcw(int x0,int y0,float ang) // rotate A -> B by angle ang around (x0,y0) CW if ang>0
{
int x,y,ix0,iy0,ix1,iy1,q;
float xx,yy,fx,fy,c,s;
// circle kernel
c=cos(-ang); s=sin(-ang);
// rotate
for (y=0;y<ys;y++)
for (x=0;x<xs;x++)
{
// offset so (0,0) is center of rotation
xx=x-x0;
yy=y-y0;
// rotate (fx,fy) by ang
fx=float((xx*c)-(yy*s));
fy=float((xx*s)+(yy*c));
// offset back and convert to ints and weights
fx+=x0; ix0=floor(fx); fx-=ix0; ix1=ix0+1; if (ix1>=xs) ix1=ix0;
fy+=y0; iy0=floor(fy); fy-=iy0; iy1=iy0+1; if (iy1>=ys) iy1=iy0;
// bilinear interpolation A[ix0+fx][iy0+fy] -> B[x][y]
if ((ix0>=0)&&(ix0<xs)&&(iy0>=0)&&(iy0<ys))
{
xx=float(A[ix0][iy0])+(float(A[ix1][iy0]-A[ix0][iy0])*fx);
yy=float(A[ix0][iy0])+(float(A[ix1][iy0]-A[ix0][iy0])*fx);
xx=xx+((yy-xx)*fy); q=xx;
} else q=0;
B[x][y]=q;
}
}
//---------------------------------------------------------------------------
此处7x7预览15度步距:
Here 7x7 preview 15 deg steps:
可能需要通过一半的细胞或其他东西稍微调整中心(中心在流血太多,我很喜欢)
It may be need a bit tweaking of the center by half of cell or something (the center is bleeding too much to my liking)
矩阵 A
是源, B
是目标...
Matrix A
is source and B
is target ...
您还可以添加所有权...
You can also add tresholding ... like:
if (q>=5) q=9; else q=0;
这篇关于如何将2D阵列以小于90°的角度旋转到最佳近似值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!