From 953f0652c4e8225343ee60be59173458cd1d6530 Mon Sep 17 00:00:00 2001 From: PatR Date: Sun, 27 Dec 2020 17:01:33 -0800 Subject: [PATCH] reformat Qt/qt_clust.cpp --- win/Qt/qt_clust.cpp | 240 ++++++++++++++++++++++---------------------- 1 file changed, 121 insertions(+), 119 deletions(-) diff --git a/win/Qt/qt_clust.cpp b/win/Qt/qt_clust.cpp index b21a53a74..d63d2d7de 100644 --- a/win/Qt/qt_clust.cpp +++ b/win/Qt/qt_clust.cpp @@ -5,163 +5,165 @@ static void include(QRect& r, const QRect& rect) { - if (rect.left()r.right()) { - r.setRight(rect.right()); - } - if (rect.top()r.bottom()) { - r.setBottom(rect.bottom()); - } + if (rect.left() < r.left()) { + r.setLeft(rect.left()); + } + if (rect.right() > r.right()) { + r.setRight(rect.right()); + } + if (rect.top() < r.top()) { + r.setTop(rect.top()); + } + if (rect.bottom() > r.bottom()) { + r.setBottom(rect.bottom()); + } } /* -A Clusterizer groups rectangles (QRects) into non-overlapping rectangles -by a merging heuristic. -*/ + * A Clusterizer groups rectangles (QRects) into non-overlapping + * rectangles by a merging heuristic. + */ Clusterizer::Clusterizer(int maxclusters) : - cluster(new QRect[maxclusters]), - count(0), - max(maxclusters) -{ } + cluster(new QRect[maxclusters]), + count(0), + max(maxclusters) +{ +} Clusterizer::~Clusterizer() { - delete [] cluster; + delete [] cluster; } void Clusterizer::clear() { - count=0; + count = 0; } void Clusterizer::add(int x, int y) { - add(QRect(x,y,1,1)); + add(QRect(x, y, 1, 1)); } void Clusterizer::add(int x, int y, int w, int h) { - add(QRect(x,y,w,h)); + add(QRect(x, y, w, h)); } void Clusterizer::add(const QRect& rect) { - QRect biggerrect(rect.x()-1,rect.y()-1,rect.width()+2,rect.height()+2); + QRect biggerrect(rect.x() - 1, rect.y() - 1, + rect.width() + 2, rect.height() + 2); - //assert(rect.width()>0 && rect.height()>0); + //assert(rect.width() > 0 && rect.height() > 0); - int cursor; + int cursor; - for (cursor=0; cursor=0) { - include(cluster[cheapest],rect); - return; - } + if (cost < lowestcost) { + bool bad = false; + for (int c = 0; c < count && !bad; c++) { + bad = cluster[c].intersects(larger) && c != cursor; + } + if (!bad) { + cheapest = cursor; + lowestcost = cost; + } + } + } + } + if (cheapest >= 0) { + include(cluster[cheapest], rect); + return; + } - if (count < max) { - cluster[count++]=rect; - return; - } + if (count < max) { + cluster[count++] = rect; + return; + } - // Do cheapest of: - // add to closest cluster - // do cheapest cluster merge, add to new cluster + // Do cheapest of: + // add to closest cluster + // do cheapest cluster merge, add to new cluster - lowestcost=9999999; - cheapest=-1; - for (cursor=0; cursor=0) { - include(cluster[cheapestmerge1],cluster[cheapestmerge2]); - cluster[cheapestmerge2]=cluster[count--]; - } else { - // if (!cheapest) debugRectangles(rect); - include(cluster[cheapest],rect); - } + if (cheapestmerge1 >= 0) { + include(cluster[cheapestmerge1], cluster[cheapestmerge2]); + cluster[cheapestmerge2] = cluster[count--]; + } else { + // if (!cheapest) debugRectangles(rect); + include(cluster[cheapest],rect); + } - // NB: clusters do not intersect (or intersection will - // overwrite). This is a result of the above algorithm, - // given the assumption that (x,y) are ordered topleft - // to bottomright. + // NB: clusters do not intersect (or intersection will + // overwrite). This is a result of the above algorithm, + // given the assumption that (x,y) are ordered topleft + // to bottomright. } const QRect& Clusterizer::operator[](int i) { - return cluster[i]; + return cluster[i]; }