{{ message }}

Instantly share code, notes, and snippets.

oisdk/MatrixRot.c

Last active Aug 29, 2015
 #include #include int main(void) { void rotate(int *mat, int side); #define Side 4 int mat[Side][Side] = { { 1, 2, 3, 4}, { 5, 6, 7, 8}, { 9, 10, 11, 12}, {13, 14, 15, 16} }; #define PrintMatrix \ for (int y = 0; y < Side; y++) { \ for (int x = 0; x < Side; x++) \ mat[y][x] < 10 ? \ printf(" %d ", mat[y][x]) : \ printf( "%d ", mat[y][x]); \ printf("\n"); \ } PrintMatrix; rotate(&mat[0][0], Side); printf("\n"); PrintMatrix; } void rotate(int *mat, const int side) { const int xLim = (side - 1) / 2, yLim = (side - 2) / 2; const int rotId = side - 1; int *from, *to, y, x, lY, lX, tY; #define Swap(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) for (x = 0; x <= xLim; ++x) for (y = 0; y <= yLim; ++y) for (lY = y, lX = x, tY = rotId - lX; tY != y || lY != x; lX = lY, lY = tY, tY = rotId - lX) { from = mat + lX + side * lY, to = mat + lY + side * tY; Swap(*from, *to); } }
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.