From 67a2d5c5dadf5dd8cce705d1719968b2da4fefec Mon Sep 17 00:00:00 2001 From: TheSaminator Date: Sun, 12 Jun 2022 14:41:49 -0400 Subject: [PATCH] Some changes to vector code --- .../game/ai/ai_optimization_util.kt | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/commonMain/kotlin/net/starshipfights/game/ai/ai_optimization_util.kt b/src/commonMain/kotlin/net/starshipfights/game/ai/ai_optimization_util.kt index cb37490..2cde6c2 100644 --- a/src/commonMain/kotlin/net/starshipfights/game/ai/ai_optimization_util.kt +++ b/src/commonMain/kotlin/net/starshipfights/game/ai/ai_optimization_util.kt @@ -2,7 +2,7 @@ package net.starshipfights.game.ai 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 @@ -16,8 +16,11 @@ val VecN.dimension: Int 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)) @@ -31,8 +34,11 @@ fun Random.nextUnitVector(size: Int): VecN { } fun Random.nextOrthonormalBasis(size: Int): List { - 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))) @@ -53,7 +59,7 @@ fun Random.nextOrthonormalBasis(size: Int): List { val VecN.isNullVector: Boolean get() { - return values.all { abs(it) < EPSILON } + return values.all { it.absoluteValue < EPSILON } } fun VecN.normalize(): VecN { -- 2.25.1