It looks like you're new here. If you want to get involved, click one of these buttons!

- 141.2K All Categories
- 103.9K Programming Languages
- 6.5K Assembler Developer
- 1.9K Basic
- 40K C and C++
- 2.9K C#
- 7.9K Delphi and Kylix
- 4 Haskell
- 9.7K Java
- 4.1K Pascal
- 1.3K Perl
- 2K PHP
- 552 Python
- 37 Ruby
- 4.4K VB.NET
- 1.6K VBA
- 20.9K Visual Basic
- 2.6K Game programming
- 317 Console programming
- 93 DirectX Game dev
- 1 Minecraft
- 112 Newbie Game Programmers
- 2 Oculus Rift
- 9K Applications
- 1.8K Computer Graphics
- 746 Computer Hardware
- 3.5K Database & SQL
- 535 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 258 XML Development
- 3.3K Classifieds
- 199 Co-operative Projects
- 199 For sale
- 190 FreeLance Software City
- 1.9K Jobs Available
- 605 Jobs Wanted
- 212 Wanted
- 2.9K Microsoft .NET
- 1.8K ASP.NET
- 1.1K .NET General
- 3.4K Miscellaneous
- 7 Join the Team
- 356 Comments on this site
- 71 Computer Emulators
- 2.1K General programming
- 187 New programming languages
- 640 Off topic board
- 226 Mobile & Wireless
- 98 Android
- 126 Palm Pilot
- 340 Multimedia
- 156 Demo programming
- 184 MP3 programming
- Bash scripts
- 27 Cloud Computing
- 53 FreeBSD
- 1.7K LINUX programming
- 371 MS-DOS
- Shell scripting
- 321 Windows CE & Pocket PC
- 4.1K Windows programming
- 944 Software Development
- 417 Algorithms
- 68 Object Orientation
- 92 Project Management
- 95 Quality & Testing
- 271 Security
- 7.7K WEB-Development
- 1.8K Active Server Pages
- 62 AJAX
- 6 Bootstrap Themes
- 55 CGI Development
- 28 ColdFusion
- 224 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 2.2K JavaScript
- 37 JQuery
- 309 WEB Servers
- 157 WEB-Services / SOAP

Matt Jacobs
Member Posts: **2**

in Algorithms

Hi,

I need an algorithm that will overlay a spiral on a square array. Basically, an x/y position is stored with respect to this square array, and if given an offset, the position will be moved in a clockwise spiral direction.

eg. a 3x3 square array will have the spiral pattern as:

012

783

654

saying the position was 2,0 then giving an offset of 3 will move it to position 5, giving it an offset of 6 will move it to position 8 etc

I've wracked my brains trying to figure this one out, and I can't seem to find any algorithms out there on the net. Any help would really be appreciated.

I need an algorithm that will overlay a spiral on a square array. Basically, an x/y position is stored with respect to this square array, and if given an offset, the position will be moved in a clockwise spiral direction.

eg. a 3x3 square array will have the spiral pattern as:

012

783

654

saying the position was 2,0 then giving an offset of 3 will move it to position 5, giving it an offset of 6 will move it to position 8 etc

I've wracked my brains trying to figure this one out, and I can't seem to find any algorithms out there on the net. Any help would really be appreciated.

Terms of use / Privacy statement / Publisher: Lars Hagelin

Programmers Heaven articles / Programmers Heaven files / Programmers Heaven uploaded content / Programmers Heaven C Sharp ebook / Operated by CommunityHeaven

© 1997-2017 Programmersheaven.com - All rights reserved.

## Comments

675[b][red]This message was edited by Josh Code at 2002-9-9 18:12:41[/red][/b][hr]

: Hi,

:

: I need an algorithm that will overlay a spiral on a square array. Basically, an x/y position is stored with respect to this square array, and if given an offset, the position will be moved in a clockwise spiral direction.

:

: eg. a 3x3 square array will have the spiral pattern as:

: 012

: 783

: 654

:

: saying the position was 2,0 then giving an offset of 3 will move it to position 5, giving it an offset of 6 will move it to position 8 etc

:

: I've wracked my brains trying to figure this one out, and I can't seem to find any algorithms out there on the net. Any help would really be appreciated.

:

Here is a way you could do it:

[b]Variables[/b]

let c = a counter variable (integer) that counts the value of the number stored in the array at a given point

let x,y = the coordinates of the current point being being processed in the array

let dx,dy = the change in x and y

let dir = the direction of motion (up, down, left, right). This is used to set the dx and dy values

let s = the length of the sides of a particullar "square" within the array. I am talking about the smaller "squares" that would be inside the parimiters of the array that approach the centre of the square. I hope this makes some sence.

let z = a counter for the loop along a straight line on one of the sprial's sides

[b]code segment[/b]

s = the length of the square array (3 for the example you provided)

dir = 0

// direction is initially right

x = 0

y = 0

// top left corner of the array

c = 0

// start the spiral at 0

Loop while the x,y is not the coordinates of the centre of the array

case dir of

0: dx = 1

dy = 0

1: dx = 0

dy = 1

2: dx = -1

dy = 0

3: dx = 0

dy = -1

s = s -1

dir = -1

end of case statement

// now the direction would be updated

add 1 to dir

z = 0

loop while z<s

set array[x,y] to c

if z=0 and dir=1 then subtract 1 from s

add dx to x

add dy to y

add 1 to z

add 1 to c

end of z-loop

end of coordinate loop

I haven't tested that but try that algorithm out. If it doesn't work, tell me about it.

89transform ordinary matrix positions to spiral positions.

123 position(p) 123

456 -> 894

789 765

Matrix position p is (row*n+col) where n is the sidelength of the square matrix. position(p) will give the

To move the value from position 6 (2,1) spiralwise 3 steps, get the new position from position(6+3)

I tried this last night and it works for all matrixes with n>0. Sorry I couldn't put this in proper algorithm terms.

' n is the sidelength of the matrix

n=

For t = 1 To n: position(t) = t: Next

turnfactor(0) = -1 / n: turnfactor(1) = n

toggle = 0

ds = n

cnt = n - 1

cnt_start = n - 1

For t = n + 1 To n * n

position(t) = position(t - 1) + ds

cnt = cnt - 1

If cnt = 0 Then

ds = ds * turnfactor(toggle)

cnt_start = cnt_start - toggle

cnt = cnt_start

toggle = toggle Xor 1

End If

Next

1: transform ordinary matrix positions to spiral positions.

:

: 123 position(p) 123

: 456 -> 894

: 789 765

:

: Matrix position p is (row*n+col) where n is the sidelength of the

: square matrix. position(p) will give the

:

: To move the value from position 6 (2,1) spiralwise 3 steps, get the

: new position from position(6+3)

:

: I tried this last night and it works for all matrixes with n>0.

: Sorry I couldn't put this in proper algorithm terms.

:

: ' n is the sidelength of the matrix

: n=

: For t = 1 To n: position(t) = t: Next

: turnfactor(0) = -1 / n: turnfactor(1) = n

: toggle = 0

: ds = n

: cnt = n - 1

: cnt_start = n - 1

:

: For t = n + 1 To n * n

: position(t) = position(t - 1) + ds

: cnt = cnt - 1

: If cnt = 0 Then

: ds = ds * turnfactor(toggle)

: cnt_start = cnt_start - toggle

: cnt = cnt_start

: toggle = toggle Xor 1

: End If

: Next

:

:

Here is a Java version of the above algorithm. However, I put

the array in reverse order for my own purpose.

private static int[] spiralArray(int dimension){

int numberOfItem = dimension*dimension;

int[] spiralArr = new int[numberOfItem];

byte toggle = 0;

int ds = dimension;

int cnt = dimension - 1;

int cntStart = dimension - 1;

for (int i = 1; i <= dimension; i++){

spiralArr[numberOfItem-i] = i;

}

for (int i = numberOfItem - dimension ; i > 0 ; i--){

spiralArr[i - 1] = spiralArr[i] + ds;

cnt = cnt - 1;

if (cnt == 0){

ds = (int)(ds * turnFactor(toggle, dimension));

cntStart = cntStart - toggle;

cnt = cntStart;

toggle = (byte)(toggle^1);

}

}

return spiralArr;

}

/**

*

* @param arg

* @param n

* @return

*/

private static float turnFactor(int arg, float n){

if (arg == 0){

return -1/n;

}else {

return n;

}

}