Skip to content

Commit

Permalink
Rotate image
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Jul 22, 2017
1 parent d0fe9c1 commit 8bd39a5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 048_rotate_image/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test rotate.c
48 changes: 48 additions & 0 deletions 048_rotate_image/rotate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>

static void rotate(int** matrix, int matrixRowSize, int matrixColSize) {
int i, j;
if (matrixRowSize != matrixColSize) {
return;
}

for (i = 0; i < matrixRowSize / 2; i++) {
int low = i, high = matrixColSize - i - 1;
for (j = low; j < high; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[matrixColSize - 1 - j][i];
matrix[matrixColSize - 1 - j][i] = matrix[matrixRowSize - 1 - i][matrixColSize - 1 - j];
matrix[matrixRowSize - 1 - i][matrixColSize - 1 - j] = matrix[j][matrixRowSize - 1 - i];
matrix[j][matrixRowSize - 1 - i] = tmp;
}
}
}

int main(int argc, char **argv)
{
if (argc != 3) {
fprintf(stderr, "Usage: ./test 3 3\n");
}

int i, j, count = 0;
int row_size = atoi(argv[1]);
int col_size = atoi(argv[2]);
int **matrix = malloc(row_size * sizeof(int *));
for (i = 0; i < row_size; i++) {
matrix[i] = malloc(col_size * sizeof(int));
for (j = 0; j < col_size; j++) {
matrix[i][j] = count++;
}
}

rotate(matrix, row_size, col_size);
for (i = 0; i < col_size; i++) {
for (j = 0; j < row_size; j++) {
printf("%02d ", matrix[i][j]);
}
putchar('\n');
}

return 0;
}
Binary file added 048_rotate_image/test
Binary file not shown.

0 comments on commit 8bd39a5

Please sign in to comment.