views:

546

answers:

3

I need a pseudocode or real code on how to do a zig zag scanning on a 2-d array using the following pattern:

alt text

I have tried to use google to find this, but can't find any answers... This pattern is usually used for image decoding

+5  A: 

You can find plenty on Rosetta Code.

Joey
+1  A: 
for i = 1 -> size
   if i is odd
      goFromLeftBottomToRightTop (i)
   else
      goFromRightTopToLeftBottom (i)
Roman
A: 

Well its fairly easy if you put your mind to it ...

In the first step you want to subtract 1 from y and add 1 to x. You then check if the new x, y position has gone off the edge of the 2d array. if so then you need to step the y back 1. You now swap the add and subtract on the x and y. So the next step you subtract 1 from x and add one to y. Repeat and you leave the array again. You now need to add one back on to x. Again reverse the x and y add/subtracts and so on until the whole image is scanned.

Goz