这是一个简单/标准的 [方形] 矩阵旋转。我用方格纸手工计算出来,然后物理旋转它。
逆时针(向左旋转),方程为:
out[7-x][y] = inp[y][x];
顺时针(向右旋转),方程为:
out[x][7-y] = inp[y][x];
...除了我们必须提取 X 维度中的位,因此我们需要一些函数来模拟位的矩阵访问。
这是一个具有必要功能的测试程序:
#include <stdio.h>
typedef unsigned char byte;
typedef void (*rot_p)(byte *dst,const byte *src);
#define MSK(_shf) (1 << (7 - (_shf)))
byte result[8];
// original matrix
byte rows[8] = {
0b00000001,
0b00000001,
0b00000001,
0b00000001,
0b00000001,
0b00000001,
0b00000001,
0b11111111
};
// counterclockwise (rotate left)
byte rows2[8] = {
0b11111111,
0b00000001,
0b00000001,
0b00000001,
0b00000001,
0b00000001,
0b00000001,
0b00000001
};
// clockwise (rotate right)
byte rows3[8] = {
0b10000000,
0b10000000,
0b10000000,
0b10000000,
0b10000000,
0b10000000,
0b10000000,
0b11111111
};
// bitget -- get bit from matrix
byte
bitget(const byte *rows,int y,int x)
{
byte val;
rows += y;
val = *rows;
val &= MSK(x);
return val;
}
// bitget -- set bit in matrix
void
bitset(byte *rows,int y,int x,byte val)
{
byte msk;
rows += y;
msk = MSK(x);
if (val)
*rows |= msk;
else
*rows &= ~msk;
}
// rotright -- rotate matrix right (clockwise)
void
rotright(byte *dst,const byte *src)
{
int x;
int y;
byte val;
for (y = 0; y < 8; ++y) {
for (x = 0; x < 8; ++x) {
val = bitget(src,y,x);
bitset(dst,x,7 - y,val);
}
}
}
// rotleft -- rotate matrix left (counterclockwise)
void
rotleft(byte *dst,const byte *src)
{
int x;
int y;
byte val;
for (y = 0; y < 8; ++y) {
for (x = 0; x < 8; ++x) {
val = bitget(src,y,x);
bitset(dst,7 - x,y,val);
}
}
}
// mtxshow -- print matrix
void
mtxshow(const byte *mtx,const char *sym,const char *tag)
{
int y;
int x;
byte val;
printf("%s/%s:\n",sym,tag);
for (y = 0; y < 8; ++y) {
printf(" ");
for (x = 0; x < 8; ++x) {
val = bitget(mtx,y,x);
val = val ? '1' : '0';
fputc(val,stdout);
}
fputc('\n',stdout);
}
}
// test -- perform test
void
test(const byte *exp,rot_p fnc,const char *tag)
{
printf("\n");
mtxshow(exp,tag,"expected");
fnc(result,rows);
mtxshow(result,tag,"actual");
}
int
main(void)
{
mtxshow(rows,"rows","orig");
test(rows2,rotleft,"rotleft");
test(rows3,rotright,"rotright");
return 0;
}
这是程序输出:
rows/orig:
00000001
00000001
00000001
00000001
00000001
00000001
00000001
11111111
rotleft/expected:
11111111
00000001
00000001
00000001
00000001
00000001
00000001
00000001
rotleft/actual:
11111111
00000001
00000001
00000001
00000001
00000001
00000001
00000001
rotright/expected:
10000000
10000000
10000000
10000000
10000000
10000000
10000000
11111111
rotright/actual:
10000000
10000000
10000000
10000000
10000000
10000000
10000000
11111111