public static int[] CalcNeighbourIndexes(int radius, int index) { HashSet <int> neighbourIndexSet = new HashSet <int>(); SideFace sideFace = CalcSideFace(radius, index); int startingIndex = CalcStartingIndex(radius); int endingIndex = CalcStartingIndex(radius + 1) - 1; int innerRadiusStartingIndex = radius == 0 ? 0 : CalcStartingIndex(radius - 1); int innerRadiusIndex = CalcNeighbourIndexFromInnerRadius(radius, index, sideFace); // 1. Get indexes from current radius if (index != startingIndex) { neighbourIndexSet.Add(index - 1); if (sideFace.Face != 0 && sideFace.RemainingLeft == 0) { neighbourIndexSet.Add(index - 2); } } if (index > endingIndex - 2) { neighbourIndexSet.Add(startingIndex); } // 2. Get indexes from inner radius neighbourIndexSet.Add(innerRadiusIndex); if (index > startingIndex + 1) { if (sideFace.Remaining > 1) { neighbourIndexSet.Add(innerRadiusIndex + 1); } if (sideFace.RemainingLeft > 0 && sideFace.Remaining > 0) { neighbourIndexSet.Add(innerRadiusIndex - 1); } } else if (index == startingIndex) { neighbourIndexSet.Add(innerRadiusStartingIndex); } else if (index == startingIndex + 1) { neighbourIndexSet.Add(startingIndex - 1); neighbourIndexSet.Add(innerRadiusStartingIndex + 1); } // 3. Cast set to int[] int[] indexes = new int[neighbourIndexSet.Count]; neighbourIndexSet.CopyTo(indexes); return(indexes); }
public static int CalcNeighbourIndexFromInnerRadius(int radius, int index, SideFace sideFace) { if (radius < 2) { return(0); } int startingIndex = CalcStartingIndex(radius); int endingIndex = CalcStartingIndex(radius + 1) - 1; if (index == startingIndex || index == endingIndex) { return(startingIndex - 1); } int indexOffset = (CIRCUMFERENCE_COEF + 2 * sideFace.Face + 1) + CIRCUMFERENCE_COEF * (radius - 2); if (sideFace.Remaining == 0) { indexOffset++; } return(index - indexOffset); }