Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Rotating a matrix

Saloon Keeper
Posts: 4355
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was doing some exercise where I frequently needed to rotate or flip a 2D rectangular array (matrix).

Now, one way to do such thing is to have two nested loops, and swap the corresponding elements. But that means what I call 'a lot of index juggling'.

Suppose we have the methods 'int[] getColumn(int[][] array, int column)' and 'int[] reverse(int[] array)'.

When we rotate a matrix right, then we see that column 0 becomes row 0, albeit reversed. So our method can become:

Can you find similar methods for rotating left and flipping horizontally or vertically?
Right! We're on it! Let's get to work tiny ad!
SKIP - a book about connecting industrious people with elderly land owners
    Bookmark Topic Watch Topic
  • New Topic