spiral stuff

Blitz3D Forums/Blitz3D Beginners Area/spiral stuff

mindstorms(Posted 2006) [#1]
Is there any equation to figure out what square is next on a spiral like path? I have a grid, with an object in the center, and want to be able to look at the surrounding squares in a repeat...until loop to figure out which is the next open square (like this)

0     1     2    3    4    5
     -------------------------------
1   | 19 | 18 | 17 | 16 | 15 |
     ------------------------------
2   | 20 | 05 | 04 | 03 | 14 |
     ------------------------------
3   | 21 | 06 | XX | 02 | 13 |
     ---------| XX |---------------
4   | 22 | 07 | XX | 01 | 12 |
     ------------------------------
5   | 23 | 08 | 09 | 10 | 11 |
     ------------------------------

where "x" is the center object (which I need to be a variable size), and the squares checked are in the order of the numbers, continuing forever unitl a square that is not occupied crops up. Is there a way to do this? I have only come up with a formula (that only works sometimes) that looks for the object if a square is ocuppied and moves according to if it is there or not.

I am using a two dimensional array to store which squares are free and which are occupied.


BlackJumper(Posted 2006) [#2]
A simple treatment would be to number the cells and then provide a list of offsets based on the shape in the middle... this will be different for each grid array that you have (so you will need a set of lists for a 5x5 that is different for a 10x10)

If you don't have too many different possible shapes in the middle it will probably be easier to create a bunch of lookup tables than it is to create a generic algorithm.

(Well, that's my view, but I don't know exactly what you are trying to achieve.)



so the order of search is....

For a single cell
===========
X+1, X-grid+1, X-grid, X-grid-1, X-1, X+grid-1, X+grid, X+grid+1, ....

For a 2 high cell
============
X+1, X-grid+1, X-2*grid+1, X-2*grid, X-2*grid-1, X-grid-1, ....


There probably is an algorithm in there, and the treatment above obviosly ignores what happens at the edge of the array, but hopefully this is a step in the right direction...


H&K(Posted 2006) [#3]
Make an array of coordinate pairs the size of thee map, each pair points to the next one.


mindstorms(Posted 2006) [#4]
It is a building game where I need to create things around another, but I don't want the things to overlap each other if the player does not move them. I also don't know where the "parent" object will be, as it is also placed by the player. I have about 30 of these "parents". Other games like mine do this, and I thought it would be reletively simple to implement it...I spent about an hour with a math teacher and it still does not work...There just has to be a better way


mindstorms(Posted 2006) [#5]
Would it work to use H&K's suggestion of putting the sequence in an array and then placing the array "map" ontop of where it needs to be, then take the next available square. It would require only one array, and seems to be what I want. Would it work to put the whole sequence in and then decide if the building is taking up the slots as well?


H&K(Posted 2006) [#6]
Mind, there are a million ways to do what you want. For example a pointer from each map square saying (up,left,right or down)or my Array overlay suggestion, Blacks algorithm (to name but Three).

You need to pick one and program it. (I would go for Blacks if I had the time, but I would make XX 0,0 and have negatives)


mindstorms(Posted 2006) [#7]
What is Black's? I can't seem to find a definition anywhere in the code archives and google...


H&K(Posted 2006) [#8]
blackJumper. Post one


mindstorms(Posted 2006) [#9]
Thanks everyone for helping! I think I will go with Blacks.