# Introduction to 2D Linux Game Programming/Algorithms/Simple Software Transformations/90° Image Rotation

### 90° Image Rotation

In 2D games it's often necessary to rotate an image by 90 degrees. This algorithm does so quickly and easily.

### The Source:

```// Rotate90.cpp
#include <iostream>
#include "include/S.h"
#include <string>
#include <sstream>
using namespace std;

int main()
{
// All rotations are clockwise
// rotate = 0 - do nothing
// rotate = 1 - rotate 90
// rotate = 2 - rotate 180
// rotate = 3 - rotate 270
// const int rotate;
const int width = S::sw();         // The original Array's width
cout << "\n";
const int height = S::sh(width);   // The original Array's height
const int widthheight = width * height;
int source[widthheight];
int dest[widthheight];
int rotate;

for(int x = 0; x < widthheight; x++)
source[x]=x;

cout << "\n\n";
S::print_rect(width, height, &source[0]);

cout << "\nTo rotate clockwise by 0   degrees enter 0."
<< "\nTo rotate clockwise by 90  degrees enter 1."
<< "\nTo rotate clockwise by 180 degrees enter 2."
<< "\nTo rotate clockwise by 270 degrees enter 3: ";
cin >> rotate;
cout << "\n\n";

switch(rotate)
{
// Do nothing
case 0:
S::print_rect(width, height, &source[0]);
break;

// Rotate 90
case 1:
for(int h = 0, dest_col = height - 1; h < height; ++h, --dest_col)
{
for(int w = 0; w < width; w++)
{
dest[ ( w * height ) + dest_col] = source[h*width + w];
}
}

S::print_rect(height, width, &dest[0]);
break;

// Rotate 180
case 2:
for( int h=0, dest_row=(height-1); h < height; --dest_row, ++h )
{
for ( int w=0, dest_col=(width-1); w < width; ++w, --dest_col )
{
dest[ dest_row * width + dest_col] = source[h*width + w];
}
}

S::print_rect(width, height, &dest[0]);
break;

// Rotate 270
case 3:
for(int h = 0, dest_col=0; h < height; ++dest_col, ++h)
{
for(int w=0, dest_row=width-1; w < width; --dest_row, ++w)
{
dest[(dest_row * height) + dest_col] = source[ h * width + w];
}
}

S::print_rect(height, width, &dest[0]);
break;

// Out of range, do nothing
default:
break;
}

cout << endl;
return 0;
}
```