Page 1 of 1

Movement in a square spiral

Posted: Thu Apr 06, 2017 12:04 pm
by Seeeks
I think this works, but if someone has the time, could someone check this for errors? The meaning is to display x and y change when you start from origo at step 0 and start moving in a clockwise spiral.

Code: Select all

function countSpiral($step) {
	$sequence = ceil(sqrt($step));//1 =1, 2...4 = 2, 5...9 = 3, 10...16 = 4
	$change = $sequence % 2;
	
	if ($change==0) {
		//even, these aren't final because they don't account for the upright part yet. That's calculated below
		$ychange = floor($sequence/2);
	}
	else {
		//odd
		$ychange = floor(($sequence-1)/2) * -1;
	}
	
	$straight_part = $sequence+1;
	$upright_part = $sequence-2;
	
	$behind = pow($sequence, 2) - $step;
	
	if ($behind<$straight_part) {
		//The x is in the straight part
		if ($ychange<0) $xchange = $ychange*-1 - $behind + 1;
		else if ($ychange>0) $xchange = $ychange*-1 + $behind;
		else $xchange = 1 - $behind;//This takes step 0 into account
	}
	else {
		$xchange = $ychange;
		//The x is in the upright part
		if ($ychange>0) {
			$ychange-=$behind-$straight_part+1;
		}
		else if ($ychange<0) {
			$ychange+=$behind-$straight_part+1;
		}
	}
	
	return array(
		"xchange" => $xchange,
		"ychange" => $ychange
		);
}

Re: Movement in a square spiral

Posted: Thu Apr 06, 2017 1:46 pm
by requinix
1. Which direction does the spiral move when it first leaves the origin?
2. I assume you mean a spiral like

Code: Select all

    -2  -1   0   1   2
   +---+---+---+---+---+
2  | /---------------\ |
   + | +   +   +   + | +
1  | |   /-------\   | |
   + | + | +   + | + | +
0  | |   |   O---/   | |
   + | + | +   +   + | +
-1 | |   \-----------/ |
   + | +   +   +   +   +
-2 | \---------------> |
   +---+---+---+---+---+

0,0 -> 1,0 -> 1,1 -> 0,1 -> -1,1 -> -1,0 ...
(if it leaves to the right)

Re: Movement in a square spiral

Posted: Thu Apr 06, 2017 3:08 pm
by Seeeks
I have the y axis reversed so minus is on top and plus is at the bottom. Sorry if it's confusing, that's just how the coordinates are set up in my project. It originates from image files starting the counting from the upper corner. I should've been clearer.

Edit: I realized it displays step 0 wrong, but that's not critical. Technically it shouldn't be called with values less than 1.

1: x: 1, y: -0

2: x: 1, y: 1

3: x: 0, y: 1

4: x: -1, y: 1

5: x: -1, y: 0

6: x: -1, y: -1

7: x: 0, y: -1

8: x: 1, y: -1

9: x: 2, y: -1

10: x: 2, y: 0

11: x: 2, y: 1

12: x: 2, y: 2

13: x: 1, y: 2

14: x: 0, y: 2

15: x: -1, y: 2

16: x: -2, y: 2

17: x: -2, y: 1

18: x: -2, y: 0

19: x: -2, y: -1

20: x: -2, y: -2

21: x: -1, y: -2

22: x: 0, y: -2

23: x: 1, y: -2

24: x: 2, y: -2

25: x: 3, y: -2

26: x: 3, y: -1

27: x: 3, y: 0

28: x: 3, y: 1

29: x: 3, y: 2

30: x: 3, y: 3

31: x: 2, y: 3

32: x: 1, y: 3

33: x: 0, y: 3

34: x: -1, y: 3

35: x: -2, y: 3

36: x: -3, y: 3

37: x: -3, y: 2

38: x: -3, y: 1

39: x: -3, y: 0

40: x: -3, y: -1

41: x: -3, y: -2

42: x: -3, y: -3

43: x: -2, y: -3

44: x: -1, y: -3

45: x: 0, y: -3

46: x: 1, y: -3

47: x: 2, y: -3

48: x: 3, y: -3

Re: Movement in a square spiral

Posted: Thu Apr 06, 2017 5:36 pm
by requinix
Seeeks wrote:I have the y axis reversed so minus is on top and plus is at the bottom. Sorry if it's confusing, that's just how the coordinates are set up in my project.
That's fine, it's typical for work involving images or graphics.

What you're doing looks like the mathematical approach I'm coming up with now (which starts with identifying the straight bits at n^2-1) so if it's working already then I believe it is correct.