day(05): introduce helper for creating range
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
19ee5441e5
commit
0492a86f08
1 changed files with 8 additions and 2 deletions
|
@ -7,6 +7,12 @@ data class Point(val x: Int, val y: Int) {
|
||||||
operator fun times(other: Int): Point = Point(other * x, other * y)
|
operator fun times(other: Int): Point = Point(other * x, other * y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getRange(from: Int, to: Int): Iterable<Int> = if (from <= to) {
|
||||||
|
from..to
|
||||||
|
} else {
|
||||||
|
(to..from).reversed()
|
||||||
|
}
|
||||||
|
|
||||||
data class Vector(val from: Point, val to: Point) {
|
data class Vector(val from: Point, val to: Point) {
|
||||||
val horizontal: Boolean
|
val horizontal: Boolean
|
||||||
get() = from.y == to.y
|
get() = from.y == to.y
|
||||||
|
@ -18,10 +24,10 @@ data class Vector(val from: Point, val to: Point) {
|
||||||
get() = horizontal || vertical
|
get() = horizontal || vertical
|
||||||
|
|
||||||
private val xs: Iterable<Int>
|
private val xs: Iterable<Int>
|
||||||
get() = if (from.x <= to.x) (from.x..to.x) else (to.x..from.x).reversed()
|
get() = getRange(from.x, to.x)
|
||||||
|
|
||||||
private val ys: Iterable<Int>
|
private val ys: Iterable<Int>
|
||||||
get() = if (from.y <= to.y) (from.y..to.y) else (to.y..from.y).reversed()
|
get() = getRange(from.y, to.y)
|
||||||
|
|
||||||
val points: Iterable<Point>
|
val points: Iterable<Point>
|
||||||
get() = if (horizontalOrVertical) {
|
get() = if (horizontalOrVertical) {
|
||||||
|
|
Loading…
Reference in a new issue