Skip to content

Instantly share code, notes, and snippets.

@GeePawHill
Created December 27, 2020 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeePawHill/f282f5eacf9c8ca68c3cbd1a1857d75a to your computer and use it in GitHub Desktop.
Save GeePawHill/f282f5eacf9c8ca68c3cbd1a1857d75a to your computer and use it in GitHub Desktop.
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