From aebd5294003b5818cc8095e8c87a320f4a293792 Mon Sep 17 00:00:00 2001 From: Jack Rainy Date: Sat, 3 Jun 2023 22:07:25 +0300 Subject: [PATCH] More efficient use of the charts space (#9505) --- core/src/com/unciv/ui/components/LineChart.kt | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/core/src/com/unciv/ui/components/LineChart.kt b/core/src/com/unciv/ui/components/LineChart.kt index c1ff26d71a..7707a986ea 100644 --- a/core/src/com/unciv/ui/components/LineChart.kt +++ b/core/src/com/unciv/ui/components/LineChart.kt @@ -13,6 +13,7 @@ import com.unciv.logic.civilization.Civilization import com.unciv.ui.components.extensions.surroundWithCircle import com.unciv.ui.screens.victoryscreen.VictoryScreenCivGroup import com.unciv.ui.screens.victoryscreen.VictoryScreenCivGroup.DefeatedPlayerStyle +import kotlin.math.min import kotlin.math.ceil import kotlin.math.log10 import kotlin.math.max @@ -71,19 +72,18 @@ class LineChart( return (0 until maxLabels + 1).map { (it * stepSize) } } - private fun getNextNumberDivisibleByPowOfTen(maxValue: Int): Int { - val numberOfDigits = ceil(log10(maxValue.toDouble())).toInt() - val maxLabelValue = when { - numberOfDigits <= 0 -> 1 - else -> { - // Some examples: - // If `maxValue = 97` => `oneWithZeros = 10^(2-1) = 10 => ceil(97/10) * 10 = 100 - // If `maxValue = 567` => `oneWithZeros = 10^(3-1) = 100 => ceil(567/100) * 100 = 600 - val oneWithZeros = 10.0.pow(numberOfDigits - 1) - ceil(maxValue.toDouble() / oneWithZeros).toInt() * oneWithZeros.toInt() - } - } - return maxLabelValue + /** + * Returns the next number of power 10, with maximal step <= 100. + * Examples: 0 => 0, 3 => 10, 97 => 100, 567 => 600, 123321 => 123400 + */ + private fun getNextNumberDivisibleByPowOfTen(value: Int): Int { + if (value == 0) return 0 + val numberOfDigits = min(ceil(log10(value.toDouble())).toInt(), 3) + val oneWithZeros = 10.0.pow(numberOfDigits - 1) + // E.g., 3 => 10^(2-1) = 10 ; ceil(3 / 10) * 10 = 10 + // 567 => 10^(3-1) = 100 ; ceil(567 / 100) * 100 = 600 + // 123321 => 10^(3-1) = 100 ; ceil(123321 / 100) * 100 = 123400 + return (ceil(value / oneWithZeros) * oneWithZeros).toInt() } override fun draw(batch: Batch, parentAlpha: Float) {