我有用六角小單元製成的六邊形。每個六邊形都有一個單位六角形的hex number。前幾個編號,如:在六角形上旋轉指標
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
(最後一個是十六進制)。
您可以將此旋轉60度的倍數,以將每個索引映射到旋轉索引。這是他們順時針旋轉60度。
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
我的問題是怎麼樣?我有這兩個函數的十六進制功能和反向十六進制功能:
function hex(n) {
return 3 * +n * (+n + 1) + 1;
}
function reverse_hex(n) {
n = (+n - 1)/3;
var i = Math.floor(Math.sqrt(n));
// null if not a hex number
return i * (i + 1) === n ? i : null;
}
我可以很容易地做0度和180度的旋轉。我可以從60度旋轉幾次後得出60度的其他倍數。
function rotate(index, direction, size) {
// The unit of direction is 60 degrees. So "1" == rotate by 60 degrees.
direction = ((+direction % 6) + 6) % 6;
switch (direction) {
case 0:
return index;
case 1:
// Something?
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: // (NaN or +/-Infinity) % 6 is NaN
return null;
}
}
但我想不出一個算法來做到這一點。