diff options
Diffstat (limited to 'src/main/kotlin/util/collections/RangeUtil.kt')
-rw-r--r-- | src/main/kotlin/util/collections/RangeUtil.kt | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/main/kotlin/util/collections/RangeUtil.kt b/src/main/kotlin/util/collections/RangeUtil.kt new file mode 100644 index 0000000..a7029ac --- /dev/null +++ b/src/main/kotlin/util/collections/RangeUtil.kt @@ -0,0 +1,40 @@ +package moe.nea.firmament.util.collections + +import kotlin.math.floor + +val ClosedFloatingPointRange<Float>.centre get() = (endInclusive + start) / 2 + +fun ClosedFloatingPointRange<Float>.nonNegligibleSubSectionsAlignedWith( + interval: Float +): Iterable<Float> { + require(interval.isFinite()) + val range = this + return object : Iterable<Float> { + override fun iterator(): Iterator<Float> { + return object : FloatIterator() { + var polledValue: Float = range.start + var lastValue: Float = polledValue + + override fun nextFloat(): Float { + if (!hasNext()) throw NoSuchElementException() + lastValue = polledValue + polledValue = Float.NaN + return lastValue + } + + override fun hasNext(): Boolean { + if (!polledValue.isNaN()) { + return true + } + if (lastValue == range.endInclusive) + return false + polledValue = (floor(lastValue / interval) + 1) * interval + if (polledValue > range.endInclusive) { + polledValue = range.endInclusive + } + return true + } + } + } + } +} |