/// <summary> /// Moore-Neighbor Tracing algorithm. /// </summary> /// <param name="body">The landmass.</param> /// <param name="i">The row.</param> /// <param name="j">The column.</param> private void TraceEdge(TerrainBody body, int i, int j) { Vector2Int start = new Vector2Int(i, j); body.AddVertex(ArrayToPosition(i, j)); Vector2Int boundary = start; Vector2Int enteredFrom = new Vector2Int(i, j - 1); //Vector2Int neighbor = new Vector2Int(i - 1, j); int neighborIndex = 6; // North in MooreNeighbor array Vector2Int neighbor = MooreNeighbor[neighborIndex]; while (neighbor != start) { int row = neighbor.x; int col = neighbor.y; if (heights[row, col] > 0) { body.AddVertex(ArrayToPosition(row, col)); enteredFrom = new Vector2Int(row, col); boundary = neighbor; if (++neighborIndex > 7) { neighborIndex = 0; } neighbor = MooreNeighbor[neighborIndex]; //neighbor = NextMooreNeighbor(enteredFrom, neighbor); } else { if (++neighborIndex > 7) { neighborIndex = 0; } neighbor = MooreNeighbor[neighborIndex]; //neighbor = NextMooreNeighbor(enteredFrom, neighbor); } } if (!body.IsValid) { throw new System.Exception("Landmass was not closed!"); } }