From 6681593cb2db39b6ab35d5b44066212e9d68b979 Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Sun, 13 Apr 2025 21:41:46 +0200 Subject: Fix simulator scale for small elements (#3658) Co-authored-by: CapellanCitizen <> --- lib/gui/simulator/drawing_panel.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/gui/simulator/drawing_panel.py b/lib/gui/simulator/drawing_panel.py index d343ea5d..abe6fa0f 100644 --- a/lib/gui/simulator/drawing_panel.py +++ b/lib/gui/simulator/drawing_panel.py @@ -3,7 +3,7 @@ # Copyright (c) 2024 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. import time -from sys import platform +import math import wx from numpy import split @@ -217,15 +217,24 @@ class DrawingPanel(wx.Panel): one_mm = PIXELS_PER_MM * self.zoom scale_width = one_mm + scale_width_mm = 1 max_width = min(canvas_width * 0.5, 300) - - while scale_width > max_width: - scale_width /= 2.0 - - while scale_width < 50: - scale_width += one_mm - - scale_width_mm = int(scale_width / self.zoom / PIXELS_PER_MM) + min_width = 50 + + if scale_width > max_width: + # max_width = one_mm * 2 ^ x + # x = log2(max_width/one_mm) + exponent = math.floor(math.log2(max_width/one_mm)) + if exponent < -6: + canvas.EndLayer() + return + + scale_width = one_mm * 2 ** exponent + scale_width_mm = round(2 ** exponent, 2) + + if scale_width < min_width: + scale_width_mm = int(min_width / one_mm) + scale_width = one_mm * scale_width_mm # The scale bar looks like this: # -- cgit v1.2.3