Skip to content
Permalink
Browse files

Added GridBounds.split

  • Loading branch information
echeipesh committed Oct 3, 2017
1 parent da2216a commit 84cd3cdc87b70955e705398e788651bd6f1bad3c
@@ -286,4 +286,19 @@ case class GridBounds(colMin: Int, rowMin: Int, colMax: Int, rowMax: Int) {
other.rowMin >= rowMin &&
other.colMax <= colMax &&
other.rowMax <= rowMax

/** Split into windows, covering original GridBounds */
def split(cols: Int, rows: Int): Iterator[GridBounds] = {
for {
windowRowMin <- Iterator.range(start = rowMin, end = rowMax + 1, step = rows)
windowColMin <- Iterator.range(start = colMin, end = colMax + 1, step = cols)
} yield {
GridBounds(
colMin = windowColMin,
rowMin = windowRowMin,
colMax = math.min(windowColMin + cols - 1, colMax),
rowMax = math.min(windowRowMin + rows - 1, rowMax)
)
}
}
}
@@ -162,4 +162,36 @@ class GridBoundsSpec extends FunSpec with Matchers{
actual.sizeLong shouldBe expected.sizeLong
}
}

describe("GridBounds.split") {
it("should not split too small a GridBounds") {
val gbs = GridBounds(0,0,7,5)
val result = gbs.split(10, 15).toList
result.head should be (gbs)
}

it("should split even GridBounds") {
val gbs = GridBounds(0,0,9,9)
val result = gbs.split(5,5).toList
result.length should be (4)
result should contain allOf (
GridBounds(0,0,4,4),
GridBounds(5,0,9,4),
GridBounds(0,5,4,9),
GridBounds(5,5,9,9)
)
}

it("should split un-even GridBounds") {
val gbs = GridBounds(0,0,10,10)
val result = gbs.split(10,10).toList
result.length should be (4)
result should contain allOf (
GridBounds(0,0,9,9),
GridBounds(10,0,10,9),
GridBounds(0,10,9,10),
GridBounds(10,10,10,10)
)
}
}
}

0 comments on commit 84cd3cd

Please sign in to comment.
You can’t perform that action at this time.