• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
  • salvin francis
  • Frits Walraven
  • Piet Souris

Rotating a matrix

Posts: 4272
  • 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?
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic