I have hexagons of small hexagons. Each hexagon has a hexadecimal number of hexagon units. The first few are numbered as:
Size 1:
0
Size 2:
0 1
2 3 4
5 6
Size 3:
0 1 2
3 4 5 6
7 8 9 A B
C D E F
101112
(the latter is in hexadecimal format).
You can rotate this multiple of 60 degrees to match each index with the rotated index. That they rotate 60 degrees clockwise.
Size 1:
0
Size 2:
2 0
5 3 1
6 4
Size 3:
7 3 0
C 8 4 1
10 D 9 5 2
11 E A 6
12 F B
My question is: how? I have two functions for the hex function and the inverse hex function:
function hex(n) {
return 3 * +n * (+n + 1) + 1;
}
function reverse_hex(n) {
n = (+n - 1) / 3;
var i = Math.floor(Math.sqrt(n));
return i * (i + 1) === n ? i : null;
}
I can easily rotate 0 degrees and 180 degrees. I can get other multiples of 60 degrees just by rotating 60 degrees several times.
function rotate(index, direction, size) {
direction = ((+direction % 6) + 6) % 6;
switch (direction) {
case 0:
return index;
case 1:
return transformed_index;
case 2:
return rotate(rotate(index, 1, size), 1, size);
case 3:
return hex(size) - index - 1;
case 4:
return rotate(rotate(index, 3, size), 1, size);
case 5:
return rotate(rotate(index, 3, size), 2, size);
default:
return null;
}
}
But I can’t come up with an algorithm for this.