From df8deb5d0db402174e12b2e1f685b4ca506b1069 Mon Sep 17 00:00:00 2001 From: Allan Bazinet Date: Sat, 26 Oct 2024 06:31:47 -0700 Subject: [PATCH] Clean up x0 calculation --- plotter.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/plotter.cpp b/plotter.cpp index 69814c34..89f6b798 100644 --- a/plotter.cpp +++ b/plotter.cpp @@ -1,5 +1,7 @@ #include "plotter.h" #include +#include +#include #include #include #include @@ -41,6 +43,21 @@ namespace constexpr std::size_t VERT_DIVS = 7; + // Given a floating point value, return the fractional portion of the + // value e.g., 42.7 -> 0.7. + + template >> + T + fractionalPart(T const v) + { + T integralPart; + return std::modf(v, &integralPart); + } + + // Given the frequency span of the entire viewable plot region, return + // the frequency span that each division should occupy. + int freqPerDiv(double const fSpan) { @@ -357,10 +374,7 @@ CPlotter::drawOverlay() float const ppdV = fpd / m_freqPerPixel; float const ppdH = (float)m_h2 / VERT_DIVS; std::size_t const hdivs = fSpan / fpd + 1.9999; - - float xx0 = float(m_startFreq) / float(fpd); - xx0 = xx0 - int(xx0); - int x0 = xx0 * ppdV + 0.5; + auto const x0 = static_cast(fractionalPart((double)m_startFreq / fpd) * ppdV + 0.5); p.setPen(QPen(Qt::white, 1, Qt::DotLine));