Source Code:
GIT
ZIP
enter a positive integer of up to five digits
HEXAGON
0
Color:
sweet
Hexagon To East:
1
Hexagon To South By Southeast:
2
Hexagon To South By Southwest:
3
Hexagon To West:
4
Hexagon To North By Northwest:
5
Hexagon To North By Northeast:
6
click the arrows to navigate ->
0
1
2
3
4
5
6
I have built an endless grid of hexagons which alternate color and spiral outwards from a central point. Why did I build this? I had an idea for an online game in which players could control hexagons and invade from one hexagon to another. I envisioned allowing everyone to play and thus I needed an endlessly huge grid of hexagons that could scale, scale, scale. Eventually, I realized that my idea for a game wasn't a good idea and thus I am just publishing the magic I wrote around the hexagons which I am proud of. Enjoy. On a chessboard (a grid of squares), two colors are used in a pattern ensuring that no two squares of the same color are ever adjacent. A comparable trick may be done to ensure that no two hexagons of the same color are adjacent on a grid of hexagons if three colors are used. The code uses no database. All magic is done by calculation. I'm Tom Jaeschke of Austin, Texas. Please contact me with any questions at: tomjaeschke@tomjaeschke.com
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
How it works: Imagine the configuration for bowling ball pins. The first row has one pin, the second row as two pins, the third row has three pins, and, yes, the fourth row has four pins. What if there were ten rows. That would make fifty-five pins. How do I get the number fifty-five? It comes from multiplying the number of rows (10) by a number one greater (11) and then dividing by two. So what row would pin forty-two sit upon? Things get a little bit trickier when trying to reverse engineer how a position corresponds to a row, but I'm sure you can see that it may be done. Pin forty-two would sit on row nine. I make hexagons manifest in rings around a hexagon zero (shown in the center below). The rings have a relationship to their contents very comparable to the relationship rows of bowling ball pins have with their contents. For example, just as I may guarantee mathematically that pin forty-two sits on row nine, I may likewise guarantee that hexagon forty-two sits on ring four as shown below. The difference between rings and rows is that a ring will hold a quantity of hexagons equivalent to its number multiplied by six instead of a quantity merely equivalent to its number. Ring one has six hexagons, ring two has twelve hexagons, ring three has eighteen hexagons, ring four has twenty-four hexagons, and so on.
0
7
8
9
10
11
12
13
14
15
16
17
18
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Rings two and four are shown above and rings one and three are shown below. All ring numbers are important for determining a hexagon's position relative to hexagon zero at the center of our spiral. Ring three, and every ring divisible evenly by three, is important for another reason. Every third ring offers colors for their hexagons in an easy-to-predict repetitive pattern.
1
2
3
4
5
6
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
How do we know what colors the other hexagons are? Well, to jump ahead a little bit, the knowledge that I find vital to hold for any one hexagon beyond its number and color is the numeric values of its six neighbors. If one cannot determine that hexagon fourteen sits to the West of hexagon four then the positions in space of hexagons fourteen and four and their proximity to hexagon zero are meaningless. The relative position of any one hexagon on our endless spiral of hexagons can only exist by being relative to something else. Now that I've said that, if one knows the neighbors of a hexagon not on a third ring and also the ring number of the hexagon not on a third ring, one will almost always be able to find two neighbors on an adjacent third ring. In gauging the colors of the two neighbors (which will always be different as the two neighbors will always be touching and two adjacent hexagons may not share the same color) one may, by default, determine the color of the hexagon NOT on the third ring by knowing that it is the color that the other two colors are not. Remember, there are only three colors and no two adjacent hexagons have the same color. Hexagons on ring two may look outward from the center of the spiral to find hexagons on ring three in the name of this calculation. Hexagons on ring four may look inwards towards the spiral's center to find hexagons on ring three in the name of this calculation. There are two exceptions where this rule does not work. Firstly, I had to just "hard code" the colors for the first seven hexagons as they are not adjacent to a third ring hexagon. Secondly, the ring outside a third ring will have six hexagons that only have one neighbor on the third ring as seen below. These guys have to have their colors calculated differently, but they nonetheless fall into an easy to calculate pattern as you can probably see for yourself.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
40
44
48
52
56
60
Why do six hexagons on ring four not have two neighboring hexagons on ring three? The rings are not circular. The rings themselves are of a hexagonal shape, holding six corners. The outliers happen at the corners. Given that the rings are hexagonal, they thus have six segments running in six different directions. In determining the neighbors of a given hexagon, one must calculate which segment of a ring one is upon. The specificity of which segment one is upon may be used to determine if the hexagon to the West is on the same ring, the ring just outside the current ring, or the ring just inside the current ring. If on the same ring, the neighbor to the West will merely have a number one greater or one smaller than that of the hexagon from which we started. If we are looking into another ring, we may find the number of the hexagon to the West by finding a hexagon that sits in more or less the same position as our first hexagon on the other ring. Much of my code calculates the relative position of two hexagons in roughly the same spot on two separate rings. Below, ring three is broken up so that you may see its six distinct segments.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36