/// <summary> /// Gets the specified cell's uppermost neighbor at the specified position. /// </summary> private Cell GetNeighbor(int cellIdx, NeighborPosition position) { Cell cell = this[cellIdx]; if (cell.Column.Index == 0 && position == NeighborPosition.Left || cell.Row.Index == 0 && position == NeighborPosition.Top || cell.Row.Index + cell.MergeDown == cell.Table.Rows.Count - 1 && position == NeighborPosition.Bottom || cell.Column.Index + cell.MergeRight == cell.Table.Columns.Count - 1 && position == NeighborPosition.Right) { return(null); } switch (position) { case NeighborPosition.Top: case NeighborPosition.Left: for (int index = cellIdx - 1; index >= 0; --index) { Cell currCell = this[index]; if (IsNeighbor(cell, currCell, position)) { return(currCell); } } break; case NeighborPosition.Right: if (cellIdx + 1 < Count) { Cell cell2 = this[cellIdx + 1]; if (cell2.Row.Index == cell.Row.Index) { return(cell2); } } for (int index = cellIdx - 1; index >= 0; --index) { Cell currCell = this[index]; if (IsNeighbor(cell, currCell, position)) { return(currCell); } } break; case NeighborPosition.Bottom: for (int index = cellIdx + 1; index < Count; ++index) { Cell currCell = this[index]; if (IsNeighbor(cell, currCell, position)) { return(currCell); } } break; } return(null); }
/// <summary> /// Gets the specified cell's uppermost neighbor at the specified position. /// </summary> private Cell GetNeighbor(CellInfo cellInfo, NeighborPosition position) { Cell cell = cellInfo.Cell; switch (position) { case NeighborPosition.Left: if (cellInfo.BlockCol > 0) { return(CellInfos[cellInfo.BlockRow, cellInfo.BlockCol - 1].Cell); } break; case NeighborPosition.Right: if (cellInfo.BlockCol + cell.MergeRight < ColCount - 1) { return(CellInfos[cellInfo.BlockRow, cellInfo.BlockCol + cell.MergeRight + 1].Cell); } break; case NeighborPosition.Top: if (cellInfo.BlockRow > 0) { return(CellInfos[cellInfo.BlockRow - 1, cellInfo.BlockCol].Cell); } break; case NeighborPosition.Bottom: if (cellInfo.BlockRow + cell.MergeDown < RowCount - 1) { return(CellInfos[cellInfo.BlockRow + cell.MergeDown + 1, cellInfo.BlockCol].Cell); } break; } return(null); }
/// <summary> /// Returns whether cell2 is a neighbor of cell1 at the specified position. /// </summary> private bool IsNeighbor(Cell cell1, Cell cell2, NeighborPosition position) { bool isNeighbor = false; switch (position) { case NeighborPosition.Bottom: int bottomRowIdx = cell1.Row.Index + cell1.MergeDown + 1; isNeighbor = cell2.Row.Index == bottomRowIdx && cell2.Column.Index <= cell1.Column.Index && cell2.Column.Index + cell2.MergeRight >= cell1.Column.Index; break; case NeighborPosition.Left: int leftClmIdx = cell1.Column.Index - 1; isNeighbor = cell2.Row.Index <= cell1.Row.Index && cell2.Row.Index + cell2.MergeDown >= cell1.Row.Index && cell2.Column.Index + cell2.MergeRight == leftClmIdx; break; case NeighborPosition.Right: int rightClmIdx = cell1.Column.Index + cell1.MergeRight + 1; isNeighbor = cell2.Row.Index <= cell1.Row.Index && cell2.Row.Index + cell2.MergeDown >= cell1.Row.Index && cell2.Column.Index == rightClmIdx; break; case NeighborPosition.Top: int topRowIdx = cell1.Row.Index - 1; isNeighbor = cell2.Row.Index + cell2.MergeDown == topRowIdx && cell2.Column.Index + cell2.MergeRight >= cell1.Column.Index && cell2.Column.Index <= cell1.Column.Index; break; } return(isNeighbor); }
/// <summary> /// Returns whether cell2 is a neighbor of cell1 at the specified position. /// </summary> private bool IsNeighbor(Cell cell1, Cell cell2, NeighborPosition position) { bool isNeighbor = false; switch (position) { case NeighborPosition.Bottom: int bottomRowIdx = cell1.Row.Index + cell1.MergeDown + 1; isNeighbor = cell2.Row.Index == bottomRowIdx && cell2.Column.Index <= cell1.Column.Index && cell2.Column.Index + cell2.MergeRight >= cell1.Column.Index; break; case NeighborPosition.Left: int leftClmIdx = cell1.Column.Index - 1; isNeighbor = cell2.Row.Index <= cell1.Row.Index && cell2.Row.Index + cell2.MergeDown >= cell1.Row.Index && cell2.Column.Index + cell2.MergeRight == leftClmIdx; break; case NeighborPosition.Right: int rightClmIdx = cell1.Column.Index + cell1.MergeRight + 1; isNeighbor = cell2.Row.Index <= cell1.Row.Index && cell2.Row.Index + cell2.MergeDown >= cell1.Row.Index && cell2.Column.Index == rightClmIdx; break; case NeighborPosition.Top: int topRowIdx = cell1.Row.Index - 1; isNeighbor = cell2.Row.Index + cell2.MergeDown == topRowIdx && cell2.Column.Index + cell2.MergeRight >= cell1.Column.Index && cell2.Column.Index <= cell1.Column.Index; break; } return isNeighbor; }
/// <summary> /// Gets the specified cell's uppermost neighbor at the specified position. /// </summary> private Cell GetNeighbor(int cellIdx, NeighborPosition position) { Cell cell = this[cellIdx]; if (cell.Column.Index == 0 && position == NeighborPosition.Left || cell.Row.Index == 0 && position == NeighborPosition.Top || cell.Row.Index + cell.MergeDown == cell.Table.Rows.Count - 1 && position == NeighborPosition.Bottom || cell.Column.Index + cell.MergeRight == cell.Table.Columns.Count - 1 && position == NeighborPosition.Right) return null; switch (position) { case NeighborPosition.Top: case NeighborPosition.Left: for (int index = cellIdx - 1; index >= 0; --index) { Cell currCell = this[index]; if (IsNeighbor(cell, currCell, position)) return currCell; } break; case NeighborPosition.Right: if (cellIdx + 1 < this.Count) { Cell cell2 = this[cellIdx + 1]; if (cell2.Row.Index == cell.Row.Index) return cell2; } for (int index = cellIdx - 1; index >= 0; --index) { Cell currCell = this[index]; if (IsNeighbor(cell, currCell, position)) return currCell; } break; case NeighborPosition.Bottom: for (int index = cellIdx + 1; index < this.Count; ++index) { Cell currCell = this[index]; if (IsNeighbor(cell, currCell, position)) return currCell; } break; } return null; }