Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rotate a 2D matrix 90 clockwise 1

Status
Not open for further replies.

TheInsider

Programmer
Jul 17, 2000
796
CA
Hi,
I have a piece of code that will rotate a linear array, which is logically a 2D (50x50) bitmap, counter-clockwise 90 by degrees:
Code:
for(int y = 0; y < height; y++){
	for(int x = 0; x < width; x++){
		rotated[((width - x - 1) * height) + y] = buffer[(y * width) + x];
	}
}
The algorithm above works. I tried to come up with a formula to rotate the matrix clockwise instead; however, my formula doesn't seem to work:
Code:
...
rotated[x + ((x + 1) * width) - y] = buffer[(y * width) + x];
...
The latter seemed to work on paper when I tested it on a simple 3x4 test grid. I know this is a simple calculation, anyone know it?

Thanks,

TheInsider
 
Rotate clockwise;
Code:
rotated[height*(x+1) - y - 1] = ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top