rotate left / right

Prev Next

Function Names

rotate left, rotate right

Description

This function rotates a m x n matrix left or rightward.

The matrix must be rectangular. Consider filling short rows with blanks or zeros before rotating.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

1

Parameters

No.TypeDescription
1
input
matrix input matrix

Matrix to be transposed

Return value

TypeDescription
matrix Rotated matrix

Examples

a[] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };

echo matrix( "0", 3, '|', 'a[] = ', a[] );

echo("Rotate original matrix counterclockwise:");
l[] = rotate left( a[] );
echo matrix( "0", 3, '|', 'l[] = ', l[] );

echo("Rotate original matrix clockwise:");
r[] = rotate right( a[] );
echo matrix( "0", 3, '|', 'r[] = ', r[] );

echo("Rotate clockwise again:");
s[] = rotate right( r[] );
echo matrix( "0", 3, '|', 's[] = ', s[] );

Output

      |  1    2    3|
      |  4    5    6|
a[] = |  7    8    9|
      | 10   11   12|

Rotate original matrix counterclockwise:
      |  3    6    9   12|
l[] = |  2    5    8   11|
      |  1    4    7   10|

Rotate original matrix clockwise:
      | 10    7    4    1|
r[] = | 11    8    5    2|
      | 12    9    6    3|

Rotate clockwise again:
      | 12   11   10|
      |  9    8    7|
s[] = |  6    5    4|
      |  3    2    1|

Try it yourself: Open LIB_Function_rotate_left.b4p in B4P_Examples.zip. Decompress before use.

See also

table rotate left
table rotate right