public static IEnumerable<bool> Segmentate(Grid grid, List<List<int>> gridVectors, int minGridCount, bool minGridClear) { Grid grid_temp = grid.Copy(); gridVectors.Clear(); int vectIndex = -1; for (int index = 0; index < grid_temp.cellsCount; ++index) { if (grid_temp[index] != 0) { if (vectIndex < 0 || gridVectors[vectIndex].Count > 0) { gridVectors.Add(new List<int>()); vectIndex += 1; } List<int> gridVector = gridVectors[vectIndex]; foreach (var res in Border(grid_temp, index, gridVector)) ;// yield return res; if (GridCount(gridVector) < minGridCount)// && !AtBorder(gridVector, grid)) { if (minGridClear) { GridClear(grid, gridVector); } gridVector.Clear(); } yield return true; } } }
public void Copy(Grid grid) { this.iCount = grid.iCount; this.jCount = grid.jCount; this.cellsCount = this.iCount * this.jCount; this.cells = new int[this.cellsCount]; Array.Copy(grid.cells, this.cells, this.cellsCount); }
private void DrawGridVectorBorders(Graphics g, Grid grid, List<int> gridVector, Color color) { Brush brush = new SolidBrush(color); int count = gridVector.Count; for (int i = 0; i < count; ++i) { int index_i, index_j; grid.Index(gridVector[i], out index_i, out index_j); g.FillRectangle(brush, index_i * this.cellSize, index_j * this.cellSize, this.cellSize, this.cellSize); } }
private static bool AtBorder(List<int> gridVector, Grid grid) { bool atBorder = false; int count = gridVector.Count; for (int i = 0; i < count && !atBorder; ++i) { int index_i, index_j; grid.Index(gridVector[i], out index_i, out index_j); atBorder = index_i <= 0 || grid.iCount - 1 <= index_i || index_j <= 0 || grid.jCount - 1 <= index_j; } return atBorder; }
public void DrawGrid(Graphics g, Grid grid) { for (int i = 0; i < grid.iCount; ++i) { for (int j = 0; j < grid.jCount; ++j) { if (grid[grid.Index(i, j)] > 0) { g.FillRectangle(this.filledBrush, i * this.cellSize, j * this.cellSize, this.cellSize, this.cellSize); } } } }
public void DrawGridVectors(Graphics g, Grid grid, List<List<int>> gridVectors) { if (gridVectors.Count == 0) { return; } Random random = new Random(0); for (int i = 0; i < gridVectors.Count - 1; ++i) { DrawGridVector(g, grid, gridVectors[i], ColorFrom(random, this.filledAlpha)); } DrawGridVectorBorders(g, grid, gridVectors[gridVectors.Count - 1], this.borderColor); }
private static IEnumerable<bool> Border(Grid grid, int index, List<int> gridVector) { int iStart; int jStart; grid.Index(index, out iStart, out jStart); int iCurr = iStart; int iv = 0; int jCurr = jStart; int jv = 1; bool isExit = false; do { if (iv == 0) { gridVector.Add(grid.Index(iCurr, jCurr)); } int iNext = iCurr + iv; int jNext = jCurr + jv; int indexNext = grid.Index(iNext, jNext); bool bNext = 0 <= iNext && iNext < grid.iCount && 0 <= jNext && jNext < grid.jCount && grid[indexNext] > 0; int iLeft = iNext - jv; int jLeft = jNext + iv; int indexLeft = grid.Index(iLeft, jLeft); bool bLeft = 0 <= iLeft && iLeft < grid.iCount && 0 <= jLeft && jLeft < grid.jCount && grid[indexLeft] > 0; if (bLeft) { iCurr = iLeft; jCurr = jLeft; int ib = iv; iv = -jv; jv = ib; } else if (bNext) { iCurr = iNext; jCurr = jNext; } else { int ib = iv; iv = jv; jv = -ib; } yield return true; } while (!(iCurr == iStart && iv == 0 && jCurr == jStart && jv == 1)); if (isExit) { gridVector.Clear(); } else { gridVector.Sort(); int count = gridVector.Count; for (int i = 0; i < count; i += 2) { for (int j = gridVector[i]; j <= gridVector[i + 1]; ++j) { grid[j] = 0; } } } }
private static int GridClear(Grid grid, List<int> gridVector) { int gridCount = 0; int count = gridVector.Count; for (int i = 0; i < count; i += 2) { for (int j = gridVector[i]; j <= gridVector[i + 1]; j++) { grid[j] = 0; } } return gridCount; }
public Grid Copy() { Grid grid = new Grid(this.iCount, this.jCount); Array.Copy(this.cells, grid.cells, this.cellsCount); return grid; }