Skip to content

Instantly share code, notes, and snippets.

@oisdk
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oisdk/e7176caaabb21c19362b to your computer and use it in GitHub Desktop.
Save oisdk/e7176caaabb21c19362b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment