import net.starshipfights.game.EPSILON
import kotlin.jvm.JvmInline
-import kotlin.math.abs
+import kotlin.math.absoluteValue
import kotlin.math.sqrt
import kotlin.random.Random
fun Random.nextGaussian() = (1..12).sumOf { nextDouble() } - 6
fun Random.nextUnitVector(size: Int): VecN {
- if (size <= 0)
- throw IllegalArgumentException("Cannot have vector of zero or negative dimension!")
+ if (size < 0)
+ throw IllegalArgumentException("Cannot have vector of negative dimension!")
+
+ if (size == 0)
+ return VecN(emptyList())
if (size == 1)
return VecN(listOf(if (nextBoolean()) 1.0 else -1.0))
}
fun Random.nextOrthonormalBasis(size: Int): List<VecN> {
- if (size <= 0)
- throw IllegalArgumentException("Cannot have orthonormal basis of zero or negative dimension!")
+ if (size < 0)
+ throw IllegalArgumentException("Cannot have orthonormal basis of negative dimension!")
+
+ if (size == 0)
+ return emptyList()
if (size == 1)
return listOf(VecN(listOf(if (nextBoolean()) 1.0 else -1.0)))
val VecN.isNullVector: Boolean
get() {
- return values.all { abs(it) < EPSILON }
+ return values.all { it.absoluteValue < EPSILON }
}
fun VecN.normalize(): VecN {