views:

61

answers:

2

I'm looking for a formula or rule that will allow me to distribute n characters into a n*n grid with as perfect of a distribution as possible. Let's say we have an array of 5 characters, A through E. Here's an example of how it definitely shouldn't turn out:

A B C D E
B C D E A
C D E A B
D E A B C
E A B C D

With this pattern, the letters clump together and are not evenly spaced. See for example the diagonal lines with repeating E's and A's etc. If we were to try another pattern however:

A B C D E
D E A B C
B C D E A
E A B C D
C D E A B

Now with this pattern, all the characters are evenly spaced along the grid. For example, you're not likely to find the letter B all that close to another B on either axis.

I'm hoping there's a rule here as to how you should shift the the A B C D E configuration around for each row to produce a spread out pattern, so I can use it to calculate not only small arrays such as this one, but arrays of any size. Any ideas as to how this can be accomplished? The language I'm trying to do this in is Objective-c if that helps.

Update: This grid is using one of the rules suggested in the answers. Unfortunetly what we see is very distinct diagonal lines. For example, the  does not spread out evenly along the grid.

 B C D E F G H I J K L M N O P Q R S
J K L M N O P Q R S  B C D E F G H I
S  B C D E F G H I J K L M N O P Q R
I J K L M N O P Q R S  B C D E F G H
R S  B C D E F G H I J K L M N O P Q
H I J K L M N O P Q R S  B C D E F G
Q R S  B C D E F G H I J K L M N O P
G H I J K L M N O P Q R S  B C D E F
P Q R S  B C D E F G H I J K L M N O
F G H I J K L M N O P Q R S  B C D E
O P Q R S  B C D E F G H I J K L M N
E F G H I J K L M N O P Q R S  B C D
N O P Q R S  B C D E F G H I J K L M
D E F G H I J K L M N O P Q R S  B C
M N O P Q R S  B C D E F G H I J K L
C D E F G H I J K L M N O P Q R S  B
L M N O P Q R S  B C D E F G H I J K
B C D E F G H I J K L M N O P Q R S 
K L M N O P Q R S  B C D E F G H I J

Update2: Another version of the grid using another set of rules suggested. Similar to the example above, the characters are indeed spaced far away from each other but only on the x-axis.

 B C D E F G H I J K L
G H I J K L  B C D E F
 B C D E F G H I J K L
G H I J K L  B C D E F
 B C D E F G H I J K L
G H I J K L  B C D E F
 B C D E F G H I J K L
G H I J K L  B C D E F
 B C D E F G H I J K L
G H I J K L  B C D E F
 B C D E F G H I J K L
G H I J K L  B C D E F

Update3: Moron's suggestion seems to have done the trick (plase note his addition of using a coprime to n that's near sqrt(n)). Here's a grid plotted thanks to his answers.

 B C D E F G H I J K L M N O P Q R S 
P Q R S  B C D E F G H I J K L M N O 
L M N O P Q R S  B C D E F G H I J K 
H I J K L M N O P Q R S  B C D E F G 
D E F G H I J K L M N O P Q R S  B C 
S  B C D E F G H I J K L M N O P Q R 
O P Q R S  B C D E F G H I J K L M N 
K L M N O P Q R S  B C D E F G H I J 
G H I J K L M N O P Q R S  B C D E F 
C D E F G H I J K L M N O P Q R S  B 
R S  B C D E F G H I J K L M N O P Q 
N O P Q R S  B C D E F G H I J K L M 
J K L M N O P Q R S  B C D E F G H I 
F G H I J K L M N O P Q R S  B C D E 
B C D E F G H I J K L M N O P Q R S  
Q R S  B C D E F G H I J K L M N O P 
M N O P Q R S  B C D E F G H I J K L 
I J K L M N O P Q R S  B C D E F G H 
E F G H I J K L M N O P Q R S  B C D 
+1  A: 

You could pick a number (say k) relatively prime to n (and perhaps close to n/2 or sqrt(n) or whatever you like), and keep shifting by k.

For eg n = 8. Pick k = 3. You get

A B C D E F G H
F G H A B C D E
C D E F G H A B
H A B C D E F G
E F G H A B C D 
B C D E F G H A
G H A B C D E F
D E F G H A B C

I have assumed you don't want to see same rows appear again.

Does that work for you?

Also these might help: Latin Squares.

Moron
Thanks for your feedback! I wrote a small program to try this model out. Sometimes it works great, but about half of the times it doesn't. I used n=19, letters A through S (but replaced the letter A with the apple logo  to make it stand out more). I plotted the grid using k=10 and what becomes visibile are two diagonal lines of apple logos. The apples didn't spread out evenly across the grid.
zath
@zath: I suggest using k closer to sqrt(n). In this case for n=19, k=4 or 5 might work out well.
Moron
Wow, you are right. Using a k-value near sqrt(n) works MUCH better. I just plotted a 49x49 grid using k=8 and the distrubution might actually be perfect. Do you know why it works so much better, or should I just shrug it off as magic? :-)
zath
@zath. It takes approx n/k shifts to make a character get back close to its original position. So sqrt(n) is sort of the 'optimum' there. For instance k = sqrt(n) maximizes the value k + n/k and is big enough to not make the symmetry obvious.
Moron
A: 

As long as your array contains more than 4 letters, shifting them two to the right (like you did), will always produce results that don't touch each other. If you want some other amount, use the following guidelines.

The range of the shift amount should be within:

2 <= shift_amount <= (array.length - 2)

This is because to not "touch" a row above, a character must be at least 2 spaces away from it on the next row:

X X X X X
X X O X X
A X X X A

In this example, imagine that the O was already placed on that line. Now we need to generate the next line. The A's represent the closest points an O can be on the next line, which is 2 spaces away.

If you want the most distribution (i.e. the numbers spaced out the most) you should choose the number in the middle of this range (e.g. 3 for 6, 3 or 4 for 7, etc.).


Here are some more examples which are less about even distribution but more about if the numbers touch or not:

4x4 shifted by 2 (success): 2 <= 2 <= 2

A B C D
C D A B
A B C D
...

5x5 shifted by 3 (success): 2 <= 3 <= 3

A B C D E
D E A B C
B C D E A
E A B C D
C D E A B

5x5 shifted by 4 (failure): 2 <= 4 <!= 3

A B C D E
E A B C D
D E A B C
C D E A B
B C D E A

6x6 shifted by 2 (success): 2 <= 2 <= 4

A B C D E F
C D E F A B
E F A B C D
...

26x26 shifted by 2 (success): 2 <= 2 <= 24

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Y Z A B C D E F G H I J K L M N O P Q R S T U V W X
W X Y Z A B C D E F G H I J K L M N O P Q R S T U V
...
Senseful
Thanks for your feedback. I tried this model using your suggestion for most distrubution (choosing a number in the middle the range). The characters are indeed spaced far away from each other, but only on the x-axis. I'll update my question to show how this scenario played out.
zath