Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Line(_start:Pair<Int, Int>, _end:Pair<Int, Int>) {
- val diagonal = _start.first != _end.first && _start.second != _end.second
- private val inverted = !diagonal && (_start.first > _end.first || _start.second > _end.second)
- private val start = if (inverted) _end else _start
- private val end = if (inverted) _start else _end
- val points = when {
- diagonal -> pointsOnDiagonal()
- start.first == end.first -> (start.second..end.second).map { start.first to it }
- else -> (start.first..end.first).map { it to start.second }
- }
- private fun pointsOnDiagonal(): List<Pair<Int, Int>> {
- val first = start.first range end.first
- val second = (start.second range end.second).toList()
- return first.mapIndexed{index, x -> x to second[index]}
- }
- }
- private infix fun Int.range(other: Int) = if (this < other) (this .. other) else (this downTo other)
- private val String.asLine: Line
- get() = this.split(" -> ").map { pair -> pair.split(",").map { coord -> coord.toInt() }}
- .let { Line(it[0][0] to it[0][1], it[1][0] to it [1][1]) }
Advertisement
Add Comment
Please, Sign In to add comment