views:

539

answers:

3

I want to move through the pixels of an image, not by going line by line, column by column in the "normal" way. But begin at the center pixel and going outward in a spiral motion. But I'm not sure how to do this. Any suggestions on how this can be done?

+4  A: 

You can do this by using parametric functions, function for radius is r(t) = R, and x(t) = Rcos(t) and y(t)=Rsin(t). Do you mean something like this?

idursun
That looks like what I want. Thank you very much!
Smetad Anarkist
A: 

It would be helpful to think about this in reverse.

For example, starting at the top left corner and moving in a clockwise direction you would move along the top row, then down the right hand side, along the bottom, and up the left edge to the pixel under the starting point.

Then move along the second row, and continue in a spiral.

Depending on the dimensions of the image you will end up with either a single column of pixels or a single row of pixels and will be moving either up/down or left/right.

From this finishing point you can then follow your steps backwards and process all the pixels as you need to.

To work out your starting position mathematically you would need to know the width/height of the image as well as which pixel you would like to end on and the direction you want to be travelling in when you get to the last pixel.

Bing
A: 

Something like this should do it:

int x = width / 2;
int y = height / 2;
int left = width * height;
int dir = 0;
int cnt = 1;
int len = 2;
int[] move = { 1, 0, -1, 0, 1 };
while (left > 0) {
   if (x >= 0 && x < width && y >= 0 && y < height) {
      // here you do something with the pixel at x,y
      left--;
   }
   x += move[dir % 4];
   y += move[(dir % 4) + 1];
   if (--cnt == 0) {
      cnt = len++ / 2;
      dir++;
   }
}

If the image is not square, the spiral will continue outside the coordinates of the image until the entire image has been covered. The condition in the if statement makes sure that only coordinates that are part of the image are processed.

Guffa