evaluate data in a matrix in a spiral motion?

I trying to evaluate data in a matrix in a spiral motion, but is having a hard time, figuring out how to make it move in that motion.

any suggestion?
Last edited on
I once asked my math tutor this, he gave me a formula which calculates the x and y position from a single incrementing value.. If only I remembered what it was.
I actually kinda have an idea which involves 4 states, each states increment in different direction.

so something like this pseudo---

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int increment = 1;
state = 1;

switch (state)
 case 1: 
      for(int i = 0; i<= increment;i++)
{
  cout << matrix[m-i][n] << endl;

}

state = 2;
break;

case 2 :
      for(int i = 0; i<= increment;i++)
{
  cout << matrix[m][n+i] << endl;

}
increment++;
state = 3;

case 3:

      for(int i = 0; i<= increment;i++)
{
  cout << matrix[m+i][n] << endl;

}
state = 4;

case 4:
      for(int i = 0; i<= increment;i++)
{
  cout << matrix[m][n-i] << endl;

}
increment++;
state = 1;



would it work ?
Last edited on
Top row: x
Bottom row: x+h
Left column: y
Right column: y+k

Let say that you are in the upper left corner. [x][y]
You proceed right over all columns to the upper right corner. [x][y+k]
Top row: x+1
Then you go down to lower right corner, from [x+1][y+k] to [x+h][y+k]
Right column: y+k-1
Leftwards to lower left, from [x+h][y+k-1] to [x+h][y]
Bottom row: x+h-1
Upwards, from [x+h-1][y] to [x+1][y].
Left column: y+1

Notice how after completion of whole edge, the range was reduced by one?
How after completion of a circle the upper left corner is now at [x+1][y+1]?
That you can repeat the circle, until you are done?

Oh wait, was it from center or to center?
Your start point will always be seen as center point for the spiral motion.


like this
http://i41.tinypic.com/24dmseu.png
Last edited on
Topic archived. No new replies allowed.