Skip to content

Instantly share code, notes, and snippets.

@GeePawHill

GeePawHill/Area.kt

Created Dec 27, 2020
Embed
What would you like to do?
To TDD Or Not To TDD? Round 2
package org.geepawhill.dungeon
import java.lang.Integer.max
import java.lang.Integer.min
data class Area(val west: Int, val north: Int, val east: Int, val south: Int) {
val longest: Int get() = max(east - west, south - north)
val size = (east - west) * (south - north)
fun margin(amount: Int): Area {
return Area(west - amount, north - amount, east + amount, south + amount)
}
fun intersects(other: Area): Boolean {
return !(south < other.north || north > other.south || west > other.east || east < other.west)
}
fun contains(coords: Coords): Boolean {
return intersects(Area(coords.x, coords.y, coords.x, coords.y))
}
fun fill(map: Map, type: CellType) {
for (c in west..east) {
for (r in north..south) map[c, r] = type
}
}
fun union(other: Area): Area =
Area(min(west, other.west), min(north, other.north), max(east, other.east), max(south, other.south))
override fun toString(): String = "($west,$north) - ($east,$south)"
companion object {
fun normalized(start: Coords, end: Coords) =
Area(min(start.x, end.x), min(start.y, end.y), max(start.x, end.x), max(start.y, end.y))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment