Skip to content

Instantly share code, notes, and snippets.

@lokedhs
Created February 1, 2022 13:46
Show Gist options
  • Save lokedhs/dc6a7992df59e52980b398aa28111544 to your computer and use it in GitHub Desktop.
Save lokedhs/dc6a7992df59e52980b398aa28111544 to your computer and use it in GitHub Desktop.
data class SelectedArea(val x: Int, val y: Int, val width: Int, val height: Int) {
companion object {
fun computeSelectedArea(selectedCells: ObservableList<TablePosition<*, *>>): SelectedArea? {
if (selectedCells.size == 0) {
return null
}
val minCol = selectedCells.minOf { it.column }
val maxCol = selectedCells.maxOf { it.column }
val minRow = selectedCells.minOf { it.row }
val maxRow = selectedCells.maxOf { it.row }
val numCols = (maxCol - minCol) + 1
val numRows = (maxRow - minRow) + 1
val markers = IntArray(numRows * numCols)
selectedCells.forEach { p ->
markers[((p.row - minRow) * numCols) + (p.column - minCol)]++
}
if (!markers.all { it == 1 }) {
return null
}
return SelectedArea(minCol, minRow, numCols, numRows)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment