adapters: nearest-cell distance in double

This commit is contained in:
Martino Ferrari committed 2026-11-03 19:28:24 +01:00
1 parent e5b7faadec
commit 73f610b136
2 files changed
+12 -10

No files matched your search

+6 -5
View File
@@ -556,16 +556,17 @@ u32 Host::row_in(const std::vector<Rect>& 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<f32>::infinity();
double best = std::numeric_limits<double>::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;
+6 -5
View File
@@ -326,16 +326,17 @@ u32 PlotArea::row_in(const std::vector<Rect>& 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<f32>::infinity();
double best = std::numeric_limits<double>::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;