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

- 141.2K All Categories
- 104.5K Programming Languages
- 6.4K Assembler Developer
- 1.8K Basic
- 39.8K C and C++
- 4.3K C#
- 7.9K Delphi and Kylix
- 4 Haskell
- 9.6K Java
- 4.1K Pascal
- 1.3K Perl
- 2K PHP
- 509 Python
- 48 Ruby
- 4.3K VB.NET
- 1.6K VBA
- 20.8K Visual Basic
- 2.6K Game programming
- 311 Console programming
- 89 DirectX Game dev
- 1 Minecraft
- 110 Newbie Game Programmers
- 2 Oculus Rift
- 8.9K Applications
- 1.8K Computer Graphics
- 726 Computer Hardware
- 3.4K Database & SQL
- 522 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 256 XML Development
- 3.3K Classifieds
- 195 Co-operative Projects
- 182 For sale
- 189 FreeLance Software City
- 1.9K Jobs Available
- 600 Jobs Wanted
- 201 Wanted
- 2.9K Microsoft .NET
- 1.7K ASP.NET
- 1.1K .NET General
- 3.2K Miscellaneous
- 3 Join the Team
- 0 User Profiles
- 349 Comments on this site
- 59 Computer Emulators
- 2K General programming
- 178 New programming languages
- 609 Off topic board
- 164 Mobile & Wireless
- 38 Android
- 124 Palm Pilot
- 335 Multimedia
- 151 Demo programming
- 184 MP3 programming
- 0 Bash scripts
- 19 Cloud Computing
- 53 FreeBSD
- 1.7K LINUX programming
- 367 MS-DOS
- 0 Shell scripting
- 320 Windows CE & Pocket PC
- 4.1K Windows programming
- 885 Software Development
- 405 Algorithms
- 68 Object Orientation
- 87 Project Management
- 90 Quality & Testing
- 234 Security
- 7.5K WEB-Development
- 1.8K Active Server Pages
- 61 AJAX
- 2 Bootstrap Themes
- 55 CGI Development
- 19 ColdFusion
- 222 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 2.2K JavaScript
- 33 JQuery
- 287 WEB Servers
- 145 WEB-Services / SOAP

Matt Jacobs
Posts: **2**Member

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 LLC

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

## Comments

675Member[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.

89Membertransform 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

1Member: 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;

}

}