Skip to content
Advertisement

PHP rotate matrix counter-clockwise

I am solving a larger problem and at one step I need to rotate a 2D array counter-clockwise.

So if I have this matrix:

JavaScript

After the rotation it will be:

JavaScript

I have found a solution to rotate it clockwise:

JavaScript

The thing is that this has to work even when a is uni-dimensional or has only one element.

So, 1 2 3 4 will become:

JavaScript

Advertisement

Answer

JavaScript

I’ll leave it as an exercise for the reader to figure out how it works.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement