c++ - Rotating a 2D pixel array by 90 degrees

C++ - Rotating a 2D pixel array by 90 degrees

Rotating a 2D pixel array (or matrix) by 90 degrees is a common operation in image processing and graphics. The rotation can be done either clockwise or counterclockwise. Here's how you can perform this operation in C++.

Rotating 90 Degrees Clockwise

To rotate a 2D matrix by 90 degrees clockwise, you can follow these steps:

  1. Transpose the matrix: Swap rows with columns.
  2. Reverse each row: Reverse the elements of each row.

Example Code

Here's a C++ function that performs a 90-degree clockwise rotation on a 2D matrix:

#include <iostream> #include <vector> void rotate90DegreesClockwise(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); int m = matrix[0].size(); // Transpose the matrix for (int i = 0; i < n; ++i) { for (int j = i; j < m; ++j) { std::swap(matrix[i][j], matrix[j][i]); } } // Reverse each row for (int i = 0; i < n; ++i) { for (int j = 0; j < m / 2; ++j) { std::swap(matrix[i][j], matrix[i][m - j - 1]); } } } void printMatrix(const std::vector<std::vector<int>>& matrix) { for (const auto& row : matrix) { for (int val : row) { std::cout << val << ' '; } std::cout << '\n'; } } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; std::cout << "Original matrix:\n"; printMatrix(matrix); rotate90DegreesClockwise(matrix); std::cout << "Matrix after 90 degrees clockwise rotation:\n"; printMatrix(matrix); return 0; } 

Explanation

  1. Transpose the Matrix:

    • Loop through the upper triangular part of the matrix and swap elements at positions (i, j) and (j, i). This swaps rows with columns.
  2. Reverse Each Row:

    • For each row, reverse the elements by swapping the start and end elements, moving towards the center.

Rotating 90 Degrees Counterclockwise

To rotate a 2D matrix by 90 degrees counterclockwise, you can follow these steps:

  1. Transpose the matrix.
  2. Reverse each column: Reverse the elements of each column.

Example Code for Counterclockwise Rotation

#include <iostream> #include <vector> void rotate90DegreesCounterclockwise(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); int m = matrix[0].size(); // Transpose the matrix for (int i = 0; i < n; ++i) { for (int j = i; j < m; ++j) { std::swap(matrix[i][j], matrix[j][i]); } } // Reverse each column for (int j = 0; j < m; ++j) { for (int i = 0; i < n / 2; ++i) { std::swap(matrix[i][j], matrix[n - i - 1][j]); } } } void printMatrix(const std::vector<std::vector<int>>& matrix) { for (const auto& row : matrix) { for (int val : row) { std::cout << val << ' '; } std::cout << '\n'; } } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; std::cout << "Original matrix:\n"; printMatrix(matrix); rotate90DegreesCounterclockwise(matrix); std::cout << "Matrix after 90 degrees counterclockwise rotation:\n"; printMatrix(matrix); return 0; } 

Explanation

  1. Transpose the Matrix: Same as for clockwise rotation.
  2. Reverse Each Column: For each column, reverse the elements by swapping the top and bottom elements, moving towards the center.

Summary

  • Clockwise Rotation: Transpose the matrix and reverse each row.
  • Counterclockwise Rotation: Transpose the matrix and reverse each column.

This method works for square matrices. For non-square matrices, you may need to adjust the dimensions or handle them differently based on the specific requirements.

Examples

  1. How to rotate a 2D array by 90 degrees clockwise in C++?

    Description: Rotate a 2D array 90 degrees clockwise by swapping elements and using a temporary array.

    Code:

    #include <iostream> #include <vector> void rotate90Clockwise(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); int m = matrix[0].size(); std::vector<std::vector<int>> rotated(m, std::vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { rotated[j][n - 1 - i] = matrix[i][j]; } } matrix = rotated; } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotate90Clockwise(matrix); for (const auto& row : matrix) { for (int val : row) { std::cout << val << " "; } std::cout << std::endl; } return 0; } 
  2. How to rotate a 2D matrix by 90 degrees counterclockwise in C++?

    Description: Rotate a 2D matrix 90 degrees counterclockwise using a similar approach with a temporary matrix.

    Code:

    #include <iostream> #include <vector> void rotate90Counterclockwise(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); int m = matrix[0].size(); std::vector<std::vector<int>> rotated(m, std::vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { rotated[m - 1 - j][i] = matrix[i][j]; } } matrix = rotated; } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotate90Counterclockwise(matrix); for (const auto& row : matrix) { for (int val : row) { std::cout << val << " "; } std::cout << std::endl; } return 0; } 
  3. How to rotate an n x n matrix by 90 degrees clockwise in place in C++?

    Description: Rotate an n x n matrix in place by first transposing it and then reversing each row.

    Code:

    #include <iostream> #include <vector> void rotateInPlace(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); // Transpose the matrix for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { std::swap(matrix[i][j], matrix[j][i]); } } // Reverse each row for (int i = 0; i < n; ++i) { std::reverse(matrix[i].begin(), matrix[i].end()); } } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotateInPlace(matrix); for (const auto& row : matrix) { for (int val : row) { std::cout << val << " "; } std::cout << std::endl; } return 0; } 
  4. How to rotate a non-square 2D array by 90 degrees clockwise in C++?

    Description: Handle non-square matrices (rectangular arrays) for rotation.

    Code:

    #include <iostream> #include <vector> void rotateNonSquare90Clockwise(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); int m = matrix[0].size(); std::vector<std::vector<int>> rotated(m, std::vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { rotated[j][n - 1 - i] = matrix[i][j]; } } matrix = rotated; } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3, 4}, {5, 6, 7, 8} }; rotateNonSquare90Clockwise(matrix); for (const auto& row : matrix) { for (int val : row) { std::cout << val << " "; } std::cout << std::endl; } return 0; } 
  5. How to rotate a 2D array by 180 degrees in C++?

    Description: Rotate a 2D array by 180 degrees by reversing the rows and columns.

    Code:

    #include <iostream> #include <vector> void rotate180(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); int m = matrix[0].size(); for (int i = 0; i < n / 2; ++i) { for (int j = 0; j < m; ++j) { std::swap(matrix[i][j], matrix[n - 1 - i][m - 1 - j]); } } if (n % 2 == 1) { for (int j = 0; j < m / 2; ++j) { std::swap(matrix[n / 2][j], matrix[n / 2][m - 1 - j]); } } } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotate180(matrix); for (const auto& row : matrix) { for (int val : row) { std::cout << val << " "; } std::cout << std::endl; } return 0; } 
  6. How to rotate a 2D array by 270 degrees clockwise in C++?

    Description: Rotate a 2D array by 270 degrees clockwise by rotating it 90 degrees counterclockwise three times.

    Code:

    #include <iostream> #include <vector> void rotate90Counterclockwise(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); int m = matrix[0].size(); std::vector<std::vector<int>> rotated(m, std::vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { rotated[m - 1 - j][i] = matrix[i][j]; } } matrix = rotated; } void rotate270Clockwise(std::vector<std::vector<int>>& matrix) { for (int i = 0; i < 3; ++i) { rotate90Counterclockwise(matrix); } } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotate270Clockwise(matrix); for (const auto& row : matrix) { for (int val : row) { std::cout << val << " "; } std::cout << std::endl; } return 0; } 
  7. How to rotate a 2D pixel array by 90 degrees clockwise using OpenMP in C++?

    Description: Use OpenMP to parallelize the rotation of a 2D pixel array.

    Code:

    #include <iostream> #include <vector> #include <omp.h> void rotate90ClockwiseOpenMP(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); int m = matrix[0].size(); std::vector<std::vector<int>> rotated(m, std::vector<int>(n)); #pragma omp parallel for for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { rotated[j][n - 1 - i] = matrix[i][j]; } } matrix = rotated; } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotate90ClockwiseOpenMP(matrix); for (const auto& row : matrix) { for (int val : row) { std::cout << val << " "; } std::cout << std::endl; } return 0; } 
  8. How to rotate a 2D array by 90 degrees counterclockwise using C++ STL?

    Description: Use C++ STL algorithms to rotate a 2D array 90 degrees counterclockwise.

    Code:

    #include <iostream> #include <vector> #include <algorithm> void rotate90CounterclockwiseSTL(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); int m = matrix[0].size(); std::vector<std::vector<int>> rotated(m, std::vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { rotated[m - 1 - j][i] = matrix[i][j]; } } matrix = rotated; } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotate90CounterclockwiseSTL(matrix); for (const auto& row : matrix) { for (int val : row) { std::cout << val << " "; } std::cout << std::endl; } return 0; } 
  9. How to rotate a 2D array using a different method in C++?

    Description: Explore different methods of rotating a 2D array, such as matrix transformation techniques.

    Code:

    #include <iostream> #include <vector> void rotateDifferentMethod(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); int m = matrix[0].size(); std::vector<std::vector<int>> rotated(m, std::vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { rotated[j][n - 1 - i] = matrix[i][j]; } } matrix = rotated; } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotateDifferentMethod(matrix); for (const auto& row : matrix) { for (int val : row) { std::cout << val << " "; } std::cout << std::endl; } return 0; } 
  10. How to rotate a 2D array by 90 degrees using a temporary array in C++?

    Description: Use a temporary array to perform the rotation.

    Code:

    #include <iostream> #include <vector> void rotate90DegreesUsingTempArray(std::vector<std::vector<int>>& matrix) { int n = matrix.size(); int m = matrix[0].size(); std::vector<std::vector<int>> temp(m, std::vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { temp[j][n - 1 - i] = matrix[i][j]; } } matrix = temp; } int main() { std::vector<std::vector<int>> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotate90DegreesUsingTempArray(matrix); for (const auto& row : matrix) { for (int val : row) { std::cout << val << " "; } std::cout << std::endl; } return 0; } 

More Tags

scp com-interface order-of-execution negative-lookbehind move jsonb strcpy message uitableview cpu-word

More Programming Questions

More Tax and Salary Calculators

More Genetics Calculators

More Various Measurements Units Calculators

More Financial Calculators