51 lines
1.4 KiB
Kotlin
51 lines
1.4 KiB
Kotlin
package ru.dbotthepony.kstarbound.math
|
|
|
|
import ru.dbotthepony.kstarbound.world.ChunkPos
|
|
import ru.dbotthepony.kvector.util2d.AABB
|
|
import ru.dbotthepony.kvector.util2d.AABBi
|
|
import ru.dbotthepony.kvector.vector.Vector2i
|
|
|
|
fun AABB.encasingIntAABB(): AABBi {
|
|
return AABBi(
|
|
Vector2i(roundByAbsoluteValue(mins.x), roundByAbsoluteValue(mins.y)),
|
|
Vector2i(roundByAbsoluteValue(maxs.x), roundByAbsoluteValue(maxs.y)),
|
|
)
|
|
}
|
|
|
|
fun AABB.encasingChunkPosAABB(): AABBi {
|
|
return AABBi(
|
|
Vector2i(ChunkPos.tileToChunkComponent(roundTowardsNegativeInfinity(mins.x)), ChunkPos.tileToChunkComponent(roundTowardsNegativeInfinity(mins.y))),
|
|
Vector2i(ChunkPos.tileToChunkComponent(roundTowardsPositiveInfinity(maxs.x)), ChunkPos.tileToChunkComponent(roundTowardsPositiveInfinity(maxs.y))),
|
|
)
|
|
}
|
|
|
|
private class Iterator<T>(private val aabb: AABBi, private val factory: (x: Int, y: Int) -> T) : kotlin.collections.Iterator<T> {
|
|
private var x = aabb.mins.x
|
|
private var y = aabb.mins.y
|
|
private var next = true
|
|
|
|
override fun hasNext(): Boolean {
|
|
return next
|
|
}
|
|
|
|
override fun next(): T {
|
|
if (!next)
|
|
throw IllegalStateException()
|
|
|
|
val obj = factory.invoke(x++, y)
|
|
|
|
if (x > aabb.maxs.x) {
|
|
x = aabb.mins.x
|
|
|
|
if (++y > aabb.maxs.y) {
|
|
next = false
|
|
}
|
|
}
|
|
|
|
return obj
|
|
}
|
|
}
|
|
|
|
val AABBi.vectors: kotlin.collections.Iterator<Vector2i> get() = Iterator(this, ::Vector2i)
|
|
val AABBi.chunkPositions: kotlin.collections.Iterator<ChunkPos> get() = Iterator(this, ::ChunkPos)
|