Please help with Gann Square of Nine Grid

 

Any ideas on how to fill an array in a "clockwise spiral" fashion?.
If I create a form whose seed number is 1. With an upper max of X*Y. The
array dimensions will be the square root of X*Y or Array(X,Y).
If I had another one dimensional array with the numbers 1 thru X*Y, how can
I fill the main Array, beginning with the number 1 in the center and spiraling
clockwise "outwards"?
See example below.

31 32 33 34 35 36 37
30 13 14 15 16 17 38
29 12 3 4 5 18 39
28 11 2 1 6 19 40
27 10 9 8 7 20 41
26 25 24 23 22 21 42
49 48 47 46 45 44 43

etc etc until the number X is reached or in this case X*Y
and i hope this image will give a clear idea:

Any ideas would be appreciated.
Thanks;

 
int x=12,y=12 ; //center square
int fill=1 ; //counter to fill array ;
int dir=-1 ; //direction of 'walk'
int i,s1,s2 ; //counters
bool nowstop=false ;
//side is the size of the square assumed to be an odd value
Array[x][y]=fill ;
for(s1=1;s1<=side;s1++) //s=number of steps to fill 'walking' each side 1,1,2,2,3,3,
  {
   s2=s1 ; //restrict last walk line
   if ( (s1+x) > side ) {s2=s2-1 ;nowstop=true ; }
   for(i=1;i<=s2;i++) {fill++;x=x+dir;Array[x][y]=fill ;}
   if (nowstop) break ;
   if ( (s1+y) > side ) {s2=s2-1 ;nowstop=true ; }
   for(i=1;i<=s2;i++) {fill++;y=y+dir;Array[x][y]=fill ;}
   if (nowstop) break ;
   dir=dir*-1 ;
  }
Something like this might do the trick.
 
turkm:

Any ideas on how to fill an array in a "clockwise spiral" fashion?.
If I create a form whose seed number is 1. With an upper max of X*Y. The
array dimensions will be the square root of X*Y or Array(X,Y).
If I had another one dimensional array with the numbers 1 thru X*Y, how can
I fill the main Array, beginning with the number 1 in the center and spiraling
clockwise "outwards"?
See example below.

31 32 33 34 35 36 37
30 13 14 15 16 17 38
29 12 3 4 5 18 39
28 11 2 1 6 19 40
27 10 9 8 7 20 41
26 25 24 23 22 21 42
49 48 47 46 45 44 43

etc etc until the number X is reached or in this case X*Y
and i hope this image will give a clear idea:

Any ideas would be appreciated.
Thanks;

 
   int row, col, r, c, fill=0, m=1, n=23;
   int a[n][n];
   //Centre point
   row = (n-1)/2; col=row;
   //Clockwise Spiral Begin
   fill++; a[row][col]=fill;      
   //move left
   fill++; col--; a[row][col] = fill;
   while(m<=n){      
      m += 2;
      //move up
     for(r=l; r<=(m-2); r++){fill++; row--; a[row][col]= fill;}     
     //move right 
     for(c=1; c<=(m-1); c++){fill++; col++; a[row][col]= fill;}     
      //move down
     for(r=1; r<=(m-1); r++){fill++; row++; a[row][col]= fill;}     
     //move left
     for(c=1; c<=m; c++){fill++; col--; a[row][col]=fill;}
   }     
This will work. Regards, T
 
Ickyrus:
Something like this might do the trick.


Hi Ickyrus thanks a lot it works fine but there is some problems:

1 - its not clockwise spiral

2 - i need to start number 2 on the left number 1 as it looks in the first picture

see what i got it:

and this is the indicator i do not what i have to do to make these changes above?

Files:
 
Tasleem:
This will work. Regards, T


Really thank you for your respond but unfortunatly not work and this what i got:

and this is the indicator of your code:

Files:
 
Tasleem:
This will work. Regards, T


It does one change needed I think to start with a[12][12] for n=23

row = (n+1)/2;//instead of (n-1)/2; 
col=row;













 
deVries:


It does one change needed I think to start with a[12][12] for n=23


sorry but still not work, it seems the best thing to work till now the code of ( Ickyrus ) but still not as i like, thanks to all of you guys.
 
turkm:

sorry but still not work, it seems the best thing to work till now the code of ( Ickyrus ) but still not as i like, thanks to all of you guys.


I tested the code of Tasleem it is working

 

I went by your example - just change the initial value of dir to equal 1 that should make it go clockwise.

(x,y)=(1,0) then (0,1) then (-1,0) then (0,-1) these are the vectors being added to the initial center (x,y) gradually increasing in larger steps of 1,1,2,2,3,3 to arrange for diferent directions decide the order the vectors need to be in
that is change the order of x=x+dir and y=y+dir to set the starting direction and the clock direction.

*edit I think your X direction is up and your y direction is accross where I was working with the reverse.

 
Ickyrus:

I went by your example - just change the initial value of dir to equal 1 that should make it go clockwise.

(x,y)=(1,0) then (0,1) then (-1,0) then (0,-1) these are the vectors being added to the initial center (x,y) gradually increasing in larger steps of 1,1,2,2,3,3 to arrange for diferent directions decide the order the vectors need to be in
that is change the order of x=x+dir and y=y+dir to set the starting direction and the clock direction.

*edit I think your X direction is up and your y direction is accross where I was working with the reverse.


check this version of the indicator nowwww it works very fine thanks to all of you.
Files:
Reason: