img rotate
-
hi,
wie kann ich das rotate noch effizient für unterschiedliche array groessen implementieren?
zb.: #define X 3
#define Y 4
oder
#define X 4
#define Y 3#define X 4 #define Y 4 void swap(char &a, char &b) { char tmp = a; a = b; b = tmp; } void rotateimg(char str[X][Y], int row, int col, bool left) { // transpose for(int r = 0; r < row; r++) { for(int c = r; c < col; c++) { swap(str[r][c], str[c][r]); } } if(left) { // reverse elements on row order for(int r = 0; r < row; r++) { for(int c = 0; c < col/2; c++) { swap(str[c][r], str[col-c-1][r]); } } } else { // reverse elements on row order for(int r = 0; r < row; r++) { for(int c = 0; c < col/2; c++) { swap(str[r][c], str[r][col-c-1]); } } } } int main() { char img[X][Y] = {{'a','b','c','d'}, {'e','f','g','h'}, {'i','j','k','l'}, {'m','n','o','p'}}; rotateimg(img, X, Y, true); }
-
Du postest im C++-Forum. Schonmal was von Templates gehört?
template <size_t X, size_t Y> void rotateimg(char (&str)[X][Y], int row, int col, bool left)
-
ja schon...
nur hier brauche ich trotzdem eine define oder eine const int deklaration:
char img[X][Y] = {{'a','b','c','d'}, {'e','f','g','h'}, {'i','j','k','l'}, {'m','n','o','p'}};
-
Wieso braucht deine Funktion ein row und col Parameter wenn du doch X und Y hast oder noch besser warum machst du deine Funktion von X und Y abhängig wenn du doch row und col hast?
Also Funktionsdefinition ändern in
void rotateimg(char** str, int row, int col, bool left)
-
Koppler schrieb:
Wieso braucht deine Funktion ein row und col Parameter wenn du doch X und Y hast oder noch besser warum machst du deine Funktion von X und Y abhängig wenn du doch row und col hast?
Also Funktionsdefinition ändern in
void rotateimg(char** str, int row, int col, bool left)wie übergebe ich das char img[X][Y] an die funktion? void rotateimg(char** str ?
-
void rotateimg(char (*p)[Y]) {}
-
void rotateimg(char* img, size_t columns, size_t rows, ...) { size_t x = ...; size_t y = ...; char pixel = img[y * columns + x]; }