From 305e6f0a5c63d7d11a026aa41f366d02176831a2 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Wed, 11 Aug 2021 07:14:00 -0400 Subject: [PATCH] Clean up eachNearestGridPos in RackWidget. --- src/app/RackWidget.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/app/RackWidget.cpp b/src/app/RackWidget.cpp index 37c5bdd8..aebb9205 100644 --- a/src/app/RackWidget.cpp +++ b/src/app/RackWidget.cpp @@ -618,18 +618,18 @@ bool RackWidget::requestModulePos(ModuleWidget* mw, math::Vec pos) { return true; } -static void eachNearestGridPos(math::Vec pos, std::function f) { +static math::Vec eachNearestGridPos(math::Vec pos, std::function f) { // Dijkstra's algorithm to generate a sorted list of Vecs closest to `pos`. // Comparison of distance of Vecs to `pos` - auto cmpNearest = [&](const math::Vec & a, const math::Vec & b) { + auto cmpNearest = [&](const math::Vec& a, const math::Vec& b) { return a.minus(pos).square() > b.minus(pos).square(); }; // Comparison of dictionary order of Vecs - auto cmp = [&](const math::Vec & a, const math::Vec & b) { - if (a.x != b.x) - return a.x < b.x; - return a.y < b.y; + auto cmp = [](const math::Vec& a, const math::Vec& b) { + if (a.y != b.y) + return a.y < b.y; + return a.x < b.x; }; // Priority queue sorted by distance from `pos` std::priority_queue, decltype(cmpNearest)> queue(cmpNearest); @@ -643,17 +643,17 @@ static void eachNearestGridPos(math::Vec pos, std::function math::Vec testPos = queue.top(); // Check testPos if (f(testPos)) - return; + return testPos; // Move testPos to visited set queue.pop(); visited.insert(testPos); // Add adjacent Vecs static const std::vector deltas = { - math::Vec(-1, 0).mult(RACK_GRID_SIZE), math::Vec(1, 0).mult(RACK_GRID_SIZE), - math::Vec(0, -1).mult(RACK_GRID_SIZE), math::Vec(0, 1).mult(RACK_GRID_SIZE), + math::Vec(-1, 0).mult(RACK_GRID_SIZE), + math::Vec(0, -1).mult(RACK_GRID_SIZE), }; for (math::Vec delta : deltas) { math::Vec newPos = testPos.plus(delta);