Data Structuresand Algorithms Good OldJava AdvancedInterview Topics Cloud andDatabases Web Designand Development Must-knowTools Good ToKnow

## Rotate an image (specified by NxN matrix) by 90 degrees

Consider the following matrix:

If rotation is done taking its center as a pivot point, then the rotation will be like this:

So basically, the matrix is divided into 4 parts by 2 diagonals.

To complete the rotation, each part moves to its adjacent part’s place.

If we move one line to its adjacent line, then care needs to be taken that the lines get progressively smaller as we move towards the center.

Also, the line gets smaller by 2 pixels, one from the start and one from the end.

The below code achieves the above target.

No of lines (called layers below) to move in one part = N/2

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

 Name: Email: (Your email is not shared with anybody) Comment: