#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Rotatable_Image
{
private:
static const int ROTATIONS_IN_CIRCLE = 4;
std::vector<std::vector<int>> matrix;
int rotation_count = 0;
public:
Rotatable_Image(size_t size)
{
matrix.resize(size, std::vector<int>(size));
}
// rotates image 90 degrees clockwise
void rotate()
{
++rotation_count;
while (rotation_count > ROTATIONS_IN_CIRCLE)
{
rotation_count -= ROTATIONS_IN_CIRCLE;
}
}
int & get_pixel(int row, int column)
{
switch (rotation_count % ROTATIONS_IN_CIRCLE)
{
case 0:
return matrix[row][column];
case 1:
return matrix[matrix.size() - column - 1][row];
case 2:
return matrix[matrix.size() - row - 1][matrix.size() - column - 1];
case 3:
return matrix[column][matrix.size() - row - 1];
}
}
size_t size()
{
return matrix.size();
}
};
int main()
{
const size_t SIZE = 7;
Rotatable_Image image(SIZE);
cout << "******* original image ******" << endl;
int value = 0;
for (size_t i = 0; i < image.size(); ++i)
{
for (size_t j = 0; j < image.size(); ++j)
{
cout << (image.get_pixel(i, j) = value++) << ' ';
}
value++;
cout << '\n';
}
// rotate image 90 degree clockwise O(1)time & O(1)memory
image.rotate();
cout << "\n******* rotated image ******" << endl;
for (size_t i = 0; i < image.size(); ++i)
{
for (size_t j = 0; j < image.size(); ++j)
{
cout << image.get_pixel(i, j) << ' ';
}
cout << '\n';
}
}