<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Spiral algorithm needed' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Spiral algorithm needed' posted on the 'Algorithms' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2012 Programmers Heaven</copyright>
    <pubDate>Thu, 09 Feb 2012 09:17:23 -0800</pubDate>
    <lastBuildDate>Thu, 09 Feb 2012 09:17:23 -0800</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Spiral algorithm needed</title>
      <link>http://www.programmersheaven.com/mb/Algorithms/137744/137744/spiral-algorithm-needed/</link>
      <description>Hi,&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
eg. a 3x3 square array will have the spiral pattern as:&lt;br /&gt;
   012&lt;br /&gt;
   783&lt;br /&gt;
   654&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/Algorithms/137744/137744/spiral-algorithm-needed/</guid>
      <pubDate>Sun, 08 Sep 2002 02:48:45 -0800</pubDate>
      <category>Algorithms</category>
    </item>
    <item>
      <title>Re: Spiral algorithm needed</title>
      <link>http://www.programmersheaven.com/mb/Algorithms/137744/138063/re-spiral-algorithm-needed/#138063</link>
      <description>&lt;strong&gt;&lt;span style="color: Red;"&gt;This message was edited by Josh Code at  2002-9-9 18:15:8&lt;/span&gt;&lt;/strong&gt;&lt;hr /&gt;&lt;br /&gt;
&lt;strong&gt;&lt;span style="color: Red;"&gt;This message was edited by Josh Code at  2002-9-9 18:12:41&lt;/span&gt;&lt;/strong&gt;&lt;hr /&gt;&lt;br /&gt;
: Hi,&lt;br /&gt;
: &lt;br /&gt;
: 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.&lt;br /&gt;
: &lt;br /&gt;
: eg. a 3x3 square array will have the spiral pattern as:&lt;br /&gt;
:    012&lt;br /&gt;
:    783&lt;br /&gt;
:    654&lt;br /&gt;
: &lt;br /&gt;
: 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&lt;br /&gt;
: &lt;br /&gt;
: 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.&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is a way you could do it:&lt;br /&gt;
&lt;strong&gt;Variables&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
let c = a counter variable (integer) that counts the value of the number stored in the array at a given point&lt;br /&gt;
&lt;br /&gt;
let x,y = the coordinates of the current point being being processed in the array&lt;br /&gt;
&lt;br /&gt;
let dx,dy = the change in x and y&lt;br /&gt;
&lt;br /&gt;
let dir = the direction of motion (up, down, left, right).  This is used to set the dx and dy values&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
let z = a counter for the loop along a straight line on one of the sprial's sides&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;code segment&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
s = the length of the square array (3 for the example you provided)&lt;br /&gt;
dir = 0&lt;br /&gt;
// direction is initially right&lt;br /&gt;
x = 0&lt;br /&gt;
y = 0&lt;br /&gt;
// top left corner of the array&lt;br /&gt;
&lt;br /&gt;
c = 0&lt;br /&gt;
// start the spiral at 0&lt;br /&gt;
&lt;br /&gt;
Loop while the x,y is not the coordinates of the centre of the array&lt;br /&gt;
&lt;br /&gt;
case dir of&lt;br /&gt;
0: dx = 1&lt;br /&gt;
   dy = 0&lt;br /&gt;
1: dx = 0&lt;br /&gt;
   dy = 1&lt;br /&gt;
2: dx = -1&lt;br /&gt;
   dy = 0 &lt;br /&gt;
3: dx = 0&lt;br /&gt;
   dy = -1&lt;br /&gt;
   s = s -1&lt;br /&gt;
dir = -1&lt;br /&gt;
end of case statement&lt;br /&gt;
&lt;br /&gt;
// now the direction would be updated&lt;br /&gt;
&lt;br /&gt;
add 1 to dir&lt;br /&gt;
&lt;br /&gt;
z = 0&lt;br /&gt;
&lt;br /&gt;
loop while z&amp;lt;s&lt;br /&gt;
&lt;br /&gt;
set array[x,y] to c&lt;br /&gt;
&lt;br /&gt;
if z=0 and dir=1 then subtract 1 from s&lt;br /&gt;
add dx to x&lt;br /&gt;
add dy to y&lt;br /&gt;
add 1 to z&lt;br /&gt;
add 1 to c&lt;br /&gt;
&lt;br /&gt;
end of z-loop&lt;br /&gt;
&lt;br /&gt;
end of coordinate loop&lt;br /&gt;
&lt;br /&gt;
I haven't tested that but try that algorithm out.  If it doesn't work, tell me about it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/Algorithms/137744/138063/re-spiral-algorithm-needed/#138063</guid>
      <pubDate>Mon, 09 Sep 2002 18:06:08 -0800</pubDate>
      <category>Algorithms</category>
    </item>
    <item>
      <title>Re: Spiral algorithm needed</title>
      <link>http://www.programmersheaven.com/mb/Algorithms/137744/138176/re-spiral-algorithm-needed/#138176</link>
      <description>The code below creates the array "position()", which is used to &lt;br /&gt;
transform ordinary matrix positions to spiral positions.&lt;br /&gt;
&lt;br /&gt;
123   position(p)   123&lt;br /&gt;
456      -&amp;gt;         894&lt;br /&gt;
789                 765&lt;br /&gt;
&lt;br /&gt;
Matrix position p is (row*n+col) where n is the sidelength of the square matrix. position(p) will give the &lt;br /&gt;
&lt;br /&gt;
To move the value from position 6 (2,1) spiralwise 3 steps, get the new position from position(6+3)&lt;br /&gt;
&lt;br /&gt;
I tried this last night and it works for all matrixes with n&amp;gt;0. Sorry I couldn't put this in proper algorithm terms.&lt;br /&gt;
    &lt;br /&gt;
    ' n is the sidelength of the matrix&lt;br /&gt;
    n=      &lt;br /&gt;
    For t = 1 To n: position(t) = t: Next&lt;br /&gt;
    turnfactor(0) = -1 / n: turnfactor(1) = n&lt;br /&gt;
    toggle = 0&lt;br /&gt;
    ds = n&lt;br /&gt;
    cnt = n - 1&lt;br /&gt;
    cnt_start = n - 1&lt;br /&gt;
    &lt;br /&gt;
    For t = n + 1 To n * n&lt;br /&gt;
        position(t) = position(t - 1) + ds&lt;br /&gt;
        cnt = cnt - 1&lt;br /&gt;
        If cnt = 0 Then&lt;br /&gt;
            ds = ds * turnfactor(toggle)&lt;br /&gt;
            cnt_start = cnt_start - toggle&lt;br /&gt;
            cnt = cnt_start&lt;br /&gt;
            toggle = toggle Xor 1&lt;br /&gt;
        End If&lt;br /&gt;
    Next&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/Algorithms/137744/138176/re-spiral-algorithm-needed/#138176</guid>
      <pubDate>Tue, 10 Sep 2002 04:57:17 -0800</pubDate>
      <category>Algorithms</category>
    </item>
    <item>
      <title>Re: Spiral algorithm needed</title>
      <link>http://www.programmersheaven.com/mb/Algorithms/137744/382511/re-spiral-algorithm-needed/#382511</link>
      <description>: The code below creates the array "position()", which is used to &lt;br /&gt;
: transform ordinary matrix positions to spiral positions.&lt;br /&gt;
: &lt;br /&gt;
: 123   position(p)   123&lt;br /&gt;
: 456      -&amp;gt;         894&lt;br /&gt;
: 789                 765&lt;br /&gt;
: &lt;br /&gt;
: Matrix position p is (row*n+col) where n is the sidelength of the &lt;br /&gt;
: square matrix. position(p) will give the &lt;br /&gt;
: &lt;br /&gt;
: To move the value from position 6 (2,1) spiralwise 3 steps, get the &lt;br /&gt;
: new position from position(6+3)&lt;br /&gt;
: &lt;br /&gt;
: I tried this last night and it works for all matrixes with n&amp;gt;0. &lt;br /&gt;
: Sorry I couldn't put this in proper algorithm terms.&lt;br /&gt;
:     &lt;br /&gt;
:     ' n is the sidelength of the matrix&lt;br /&gt;
:     n=      &lt;br /&gt;
:     For t = 1 To n: position(t) = t: Next&lt;br /&gt;
:     turnfactor(0) = -1 / n: turnfactor(1) = n&lt;br /&gt;
:     toggle = 0&lt;br /&gt;
:     ds = n&lt;br /&gt;
:     cnt = n - 1&lt;br /&gt;
:     cnt_start = n - 1&lt;br /&gt;
:     &lt;br /&gt;
:     For t = n + 1 To n * n&lt;br /&gt;
:         position(t) = position(t - 1) + ds&lt;br /&gt;
:         cnt = cnt - 1&lt;br /&gt;
:         If cnt = 0 Then&lt;br /&gt;
:             ds = ds * turnfactor(toggle)&lt;br /&gt;
:             cnt_start = cnt_start - toggle&lt;br /&gt;
:             cnt = cnt_start&lt;br /&gt;
:             toggle = toggle Xor 1&lt;br /&gt;
:         End If&lt;br /&gt;
:     Next&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
Here is a Java version of the above algorithm.  However, I put&lt;br /&gt;
the array in reverse order for my own purpose.&lt;br /&gt;
&lt;br /&gt;
	private static int[] spiralArray(int dimension){&lt;br /&gt;
		&lt;br /&gt;
		int numberOfItem = dimension*dimension;&lt;br /&gt;
		int[] spiralArr = new int[numberOfItem];&lt;br /&gt;
		byte toggle = 0;&lt;br /&gt;
		int ds = dimension;&lt;br /&gt;
		int cnt = dimension - 1;&lt;br /&gt;
		int cntStart = dimension - 1;	&lt;br /&gt;
		for (int i = 1; i &amp;lt;= dimension; i++){&lt;br /&gt;
			spiralArr[numberOfItem-i] = i;&lt;br /&gt;
		} &lt;br /&gt;
		&lt;br /&gt;
		for (int i = numberOfItem - dimension ; i &amp;gt; 0 ; i--){&lt;br /&gt;
			spiralArr[i - 1] = spiralArr[i] + ds;&lt;br /&gt;
			cnt = cnt - 1;&lt;br /&gt;
			if (cnt == 0){&lt;br /&gt;
				ds = (int)(ds * turnFactor(toggle, dimension));&lt;br /&gt;
				cntStart = cntStart - toggle;&lt;br /&gt;
				cnt = cntStart;&lt;br /&gt;
				toggle = (byte)(toggle^1);&lt;br /&gt;
			}&lt;br /&gt;
		}		&lt;br /&gt;
		return spiralArr;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	/**&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param arg&lt;br /&gt;
	 * @param n&lt;br /&gt;
	 * @return&lt;br /&gt;
	 */&lt;br /&gt;
	private static float turnFactor(int arg, float n){&lt;br /&gt;
		if (arg == 0){			&lt;br /&gt;
			return -1/n;&lt;br /&gt;
		}else {&lt;br /&gt;
			return n; &lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/Algorithms/137744/382511/re-spiral-algorithm-needed/#382511</guid>
      <pubDate>Tue, 02 Dec 2008 20:19:42 -0800</pubDate>
      <category>Algorithms</category>
    </item>
  </channel>
</rss>
