Reputation:
Suppose:
2D array: abcdef
ghijkl
mnopqr
Stored in simple string of length width * height, thus, let's call it arr.
arr = abcdefghijklmnopqr
width = 6
height = strlen ( arr ) / width
The goal is to rotate this array by 45 degrees ( PI/4 ) and get the following result:
arr = abgchmdinejofkplqr
width = 3
height = 8
converted to 2D array: a..
bg.
chm
din
ejo
fkp
.lq
..r
I have spent a few hours trying to figure out how to make this conversion and came up with a few semi-functional solutions, but I can't get it fully working. Can you describe / write an algorithm that would solve this? Preferably in C.
Thanks for any help
Edit: This is what I've tried already
Edit2: The purpose of 45 degree rotation is to turn diagonals into lines so that they can be searched using strstr.
// this is 90 degree rotation. pretty simple
for ( i = 0; i < width * height; i++ ) {
if ( i != 0 && !(i % height) ) row++;
fieldVertical[i] = field[( ( i % height ) * width ) + row];
}
// but I just can't get my head over rotating it 45 degrees.
// this is what I've tried. It works untile 'border' is near the first edge.
row = 0;
int border = 1, rowMax = 0, col = 0; // Note that the array may be longer
// than wider and vice versa. In that case rowMax should be called colMax.
for ( i = 0; i < width * height; ) {
for ( j = 0; j < border; j++, i++ ) {
fieldCClockwise[row * width + col] = field[i];
col--;
row++;
}
col = border;
row = 0;
border++;
}
The 'border' in my code is an imaginary borderline. In the source, it is a diagonal line that separates diagonals. In the result, it would be a horizontal line between each row.
1 2 3 / 4 5
6 7 / 8 9 10
11 /12 13 14 15
Those slashes are our borderline. The algorithm shoul be pretty simple and just read riagonals: first number 1, then 2, then 6, then 3, then 7, then 11, then 4 and so on.
Upvotes: 3
Views: 5852
Reputation: 21
I would like take help of above and solve this by an example:
import pandas as pd
import numpy as np
bd = np.matrix([[44., -1., 40., 42., 40., 39., 37., 36., -1.],
[42., -1., 43., 42., 39., 39., 41., 40., 36.],
[37., 37., 37., 35., 38., 37., 37., 33., 34.],
[35., 38., -1., 35., 37., 36., 36., 35., -1.],
[36., 35., 36., 35., 34., 33., 32., 29., 28.],
[38., 37., 35., -1., 30., -1., 29., 30., 32.]])
def rotate45(array):
rot = []
for i in range(len(array)):
rot.append([0] * (len(array)+len(array[0])-1))
for j in range(len(array[i])):
rot[i][int(i + j)] = array[i][j]
return rot
df_bd = pd.DataFrame(data=np.matrix(rotate45(bd.transpose().tolist())))
df_bd = df_bd.transpose()
print df_bd
of which output will be like:
44 0 0 0 0 0 0 0 0
42 -1 0 0 0 0 0 0 0
37 -1 40 0 0 0 0 0 0
35 37 43 42 0 0 0 0 0
36 38 37 42 40 0 0 0 0
38 35 -1 35 39 39 0 0 0
0 37 36 35 38 39 37 0 0
0 0 35 35 37 37 41 36 0
0 0 0 -1 34 36 37 40 -1
0 0 0 0 30 33 36 33 36
0 0 0 0 0 -1 32 35 34
0 0 0 0 0 0 29 29 -1
0 0 0 0 0 0 0 30 28
0 0 0 0 0 0 0 0 32
Upvotes: 0
Reputation: 28251
I'd call this diagonal scanning, not rotation by 45 degrees.
In your example, the diagonals are running down-left; we can enumerate them 1, 2, ...:
123456
234567
345678
This will be the counter for the iterations of the outer loop. The inner loop will run 1, 2 or 3 iterations. In order to jump from one numbered symbol to the other, do col--; row++;
like you did, or add width-1
to a linear index:
....5. (example)
...5..
..5...
Code (untested):
char *field;
int width = 6;
int height = 3;
char *field45 = malloc(width * height);
int diag_x = 0, diag_y = 0; // coordinate at which the diagonal starts
int x, y; // coordinate of the symbol to output
while (diag_y < height)
{
x = diag_x; y = diag_y;
while (x >= 0 && y < height) // repeat until out of field
{
*field45++ = field[y * width + x]; // output current symbol
--x; ++y; // go to next symbol on the diagonal
}
// Now go to next diagonal - either right or down, whatever is possible
if (diag_x == width - 1)
++diag_y;
else
++diag_x;
}
If you want to "rotate" in another direction, you may want to change ++
to --
around the code, and maybe change bound checking in the loops to opposite.
In addition, you can replace (x,y)
coordinates by just one index (replacing ++y
by index+=width
); i used (x,y)
for clarity.
Upvotes: 1
Reputation: 801
I looked at http://en.wikipedia.org/wiki/Shear_mapping for inspiration and produced this python code:
a = [['a', 'b', 'c', 'd', 'e', 'f'],
['g', 'h', 'i', 'j', 'k', 'l'],
['m', 'n', 'o', 'p', 'q', 'r']]
m = 1 # 1/m = slope
def shear_45_ccw(array):
ret = []
for i in range(len(array)):
ret.append([0] * 8)
for j in range(len(array[i])):
ret[i][int(i + m * j)] = array[i][j]
return ret
print(shear_45_ccw(a))
produces:
[['a', 'b', 'c', 'd', 'e', 'f', 0, 0],
[0, 'g', 'h', 'i', 'j', 'k', 'l', 0],
[0, 0, 'm', 'n', 'o', 'p', 'q', 'r']]
which is something like what you want. The algorithm is hopefully readable even though its in python. The meat of it is this: ret[i][int(i + m * j)] = array[i][j]
. Good luck! I cheated when I initialized the array; you'll have to deal with that differently in C anyways.
EDIT: also, I had no idea why your result was flipped and stuff: I trust you can make the right behavior happen.
Upvotes: 2