From 73f610b13690ba29aa2cef00f6d9ca50c5dd241a Mon Sep 17 00:00:00 2001 From: Martino Ferrari Date: Tue, 3 Nov 2026 19:28:24 +0100 Subject: [PATCH] adapters: nearest-cell distance in double --- adapters/glfw/glfw_host.cpp | 11 ++++++----- adapters/gtk4/gtk_plot_area.cpp | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/adapters/glfw/glfw_host.cpp b/adapters/glfw/glfw_host.cpp index e63fdbc..0133651 100644 --- a/adapters/glfw/glfw_host.cpp +++ b/adapters/glfw/glfw_host.cpp @@ -556,16 +556,17 @@ u32 Host::row_in(const std::vector& rows, f32 x, f32 y) noexcept { // visible gap must not be routed to plot 0 by default. For a figure with one cell per // row every cell spans the figure's width, so this reduces to the nearest row by y. u32 row = 0u; - f32 best = std::numeric_limits::infinity(); + double best = std::numeric_limits::infinity(); for (u32 i = 0; i < rows.size(); ++i) { const Rect& r = rows[i]; - const f32 dx = x < r.x ? r.x - x : (x >= r.right() ? x - r.right() : 0.0f); - const f32 dy = y < r.y ? r.y - y : (y >= r.bottom() ? y - r.bottom() : 0.0f); - const f32 distance = dx * dx + dy * dy; + // In double: in f32 the dy^2 term can be swallowed by a large dx^2 (pointer far outside). + const double dx = x < r.x ? double(r.x) - x : (x >= r.right() ? double(x) - r.right() : 0.0); + const double dy = y < r.y ? double(r.y) - y : (y >= r.bottom() ? double(y) - r.bottom() : 0.0); + const double distance = dx * dx + dy * dy; if (distance < best) { best = distance; row = i; - if (distance == 0.0f) break; // inside this cell: nothing can be nearer + if (distance == 0.0) break; // inside this cell: nothing can be nearer } } return row; diff --git a/adapters/gtk4/gtk_plot_area.cpp b/adapters/gtk4/gtk_plot_area.cpp index 83e58ff..af1fa73 100644 --- a/adapters/gtk4/gtk_plot_area.cpp +++ b/adapters/gtk4/gtk_plot_area.cpp @@ -326,16 +326,17 @@ u32 PlotArea::row_in(const std::vector& rows, f32 x, f32 y) noexcept { // in a visible gap must not default to plot 0. For a figure with one cell per row // every cell spans the figure's width, so this reduces to the nearest row by y. u32 row = 0; - f32 best = std::numeric_limits::infinity(); + double best = std::numeric_limits::infinity(); for (u32 i = 0; i < rows.size(); ++i) { const Rect& r = rows[i]; - const f32 dx = x < r.x ? r.x - x : (x >= r.right() ? x - r.right() : 0.0f); - const f32 dy = y < r.y ? r.y - y : (y >= r.bottom() ? y - r.bottom() : 0.0f); - const f32 distance = dx * dx + dy * dy; + // In double: in f32 the dy^2 term can be swallowed by a large dx^2 (pointer far outside). + const double dx = x < r.x ? double(r.x) - x : (x >= r.right() ? double(x) - r.right() : 0.0); + const double dy = y < r.y ? double(r.y) - y : (y >= r.bottom() ? double(y) - r.bottom() : 0.0); + const double distance = dx * dx + dy * dy; if (distance < best) { best = distance; row = i; - if (distance == 0.0f) break; // inside this cell: nothing can be nearer + if (distance == 0.0) break; // inside this cell: nothing can be nearer } } return row;