From 973c6038562a6b37076267f679b86c05b76a6208 Mon Sep 17 00:00:00 2001 From: towzeur Date: Tue, 18 May 2021 01:30:41 +0200 Subject: [PATCH 1/2] added index_to_cubecoord and cubecoord_to_index Added two function index_to_cubecoord : index to Cube coordinates (pointy) conversion based only on its input (int index) and CubeCoord.directions cubecoord_to_index : Cube coordinates (pointy) to index conversion based only on its input (int x, int y, int z) tested and verified ```java for (int i=0; i<37; i++){ CubeCoord c = index_to_cubecoord(i); int j = cubecoord_to_index(c.x, c.y, c.z); System.out.println(String.format("%d -> (%d,%d,%d) -> %d", i, c.x, c.y, c.z, j)); assert i == j; } ``` ``` 0 -> (0,0,0) -> 0 1 -> (1,-1,0) -> 1 2 -> (1,0,-1) -> 2 3 -> (0,1,-1) -> 3 4 -> (-1,1,0) -> 4 5 -> (-1,0,1) -> 5 6 -> (0,-1,1) -> 6 7 -> (2,-2,0) -> 7 8 -> (2,-1,-1) -> 8 9 -> (2,0,-2) -> 9 10 -> (1,1,-2) -> 10 11 -> (0,2,-2) -> 11 12 -> (-1,2,-1) -> 12 13 -> (-2,2,0) -> 13 14 -> (-2,1,1) -> 14 15 -> (-2,0,2) -> 15 16 -> (-1,-1,2) -> 16 17 -> (0,-2,2) -> 17 18 -> (1,-2,1) -> 18 19 -> (3,-3,0) -> 19 20 -> (3,-2,-1) -> 20 21 -> (3,-1,-2) -> 21 22 -> (3,0,-3) -> 22 23 -> (2,1,-3) -> 23 24 -> (1,2,-3) -> 24 25 -> (0,3,-3) -> 25 26 -> (-1,3,-2) -> 26 27 -> (-2,3,-1) -> 27 28 -> (-3,3,0) -> 28 29 -> (-3,2,1) -> 29 30 -> (-3,1,2) -> 30 31 -> (-3,0,3) -> 31 32 -> (-2,-1,3) -> 32 33 -> (-1,-2,3) -> 33 34 -> (0,-3,3) -> 34 35 -> (1,-3,2) -> 35 36 -> (2,-3,1) -> 36 ``` --- .../java/com/codingame/game/CubeCoord.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/main/java/com/codingame/game/CubeCoord.java b/src/main/java/com/codingame/game/CubeCoord.java index befb1c6..252915a 100644 --- a/src/main/java/com/codingame/game/CubeCoord.java +++ b/src/main/java/com/codingame/game/CubeCoord.java @@ -77,4 +77,58 @@ public CubeCoord getOpposite() { CubeCoord oppositeCoord = new CubeCoord(-this.x, -this.y, -this.z); return oppositeCoord; } + + public static CubeCoord index_to_cubecoord(int index){ + // index to Cube coordinates (pointy) conversion. + if (index==0) return new CubeCoord(0, 0, 0); + + int i = 1 + (int) Math.floor((Math.sqrt(12 * index - 3) - 3.0) / 6.0); + int l_min = 1 + 3 * (i - 1) * i; // min index in this circle + // int l_max = 3 * i * (i + 1); // max index in this circle + int x1 = index - l_min; // linear offset + + // euclidian div : x1 = a * i + d + int o = x1 / i; // orientation index + int d = x1 % i; // direction index + + int xi = directions[o][0] * i + directions[(o+2) % 6][0] * d; + int yi = directions[o][1] * i + directions[(o+2) % 6][1] * d; + int zi = directions[o][2] * i + directions[(o+2) % 6][2] * d; + + return new CubeCoord(xi, yi, zi); + } + + + public static int cubecoord_to_index(int x, int y, int z){ + // index to Cube coordinates (pointy) conversion. + if (x==0 && y==0 && z==0) return 0; + + int I = Math.max(Math.abs(x), Math.max(Math.abs(y), Math.abs(z))); + int i_off = 1 + 3 * (I - 1) * I; + + int index; + if (x == I) + index = i_off + 0 * I - z; + else if (z == -I) + index = i_off + 1 * I + y; + else if (y == I) + index = i_off + 2 * I - x; + else if (x == -I) + index = i_off + 3 * I + z; + else if (z == I) + index = i_off + 4 * I - y; + else // if (y == -I) + index = i_off + 5 * I + x; + + return index; + } + + public static int cubecoord_to_index(CubeCoord cubecoord){ + return cubecoord_to_index(cubecoord.x, cubecoord.y, cubecoord.z); + } + + public int cubecoord_to_index(){ + return cubecoord_to_index(this.x, this.y, this.z); + } + } From 76ed2703e332162a81a1f8ab8710c865290fdd3c Mon Sep 17 00:00:00 2001 From: towzeur Date: Tue, 18 May 2021 01:35:50 +0200 Subject: [PATCH 2/2] added missing lib import (import java.lang.Math;) import java.lang.Math; --- src/main/java/com/codingame/game/CubeCoord.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/codingame/game/CubeCoord.java b/src/main/java/com/codingame/game/CubeCoord.java index 252915a..a6acde5 100644 --- a/src/main/java/com/codingame/game/CubeCoord.java +++ b/src/main/java/com/codingame/game/CubeCoord.java @@ -2,6 +2,8 @@ import com.codingame.view.Serializer; +import java.lang.Math; + public class CubeCoord { static int[][] directions = new int[][] { { 1, -1, 0 }, { +1, 0, -1 }, { 0, +1, -1 }, { -1, +1, 0 }, { -1, 0, +1 }, { 0, -1, +1 } }; int x, y, z;