File indexing completed on 2024-05-12 04:06:21

0001 /*
0002     SPDX-FileCopyrightText: 2010 Johannes Loehnert <loehnert.kde@gmx.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "pointfinder.h"
0008 
0009 #include <QLineF>
0010 
0011 PointFinder::PointFinder(int width, int height, qreal radius) {
0012     m_height = height;
0013     m_width = width;
0014     m_radius = radius;
0015     m_xbins = int(m_width / m_radius) + 1;
0016     m_ybins = int(m_height / m_radius) + 1;
0017 
0018     m_boxes = new QList<QPointF>* [m_xbins];
0019     for (int nx=0; nx < m_xbins; nx++) m_boxes[nx] = new QList<QPointF> [m_ybins];
0020 
0021 
0022 }
0023 
0024 PointFinder::~PointFinder() {
0025     for (int nx=0; nx < m_xbins; nx++) delete[] m_boxes[nx];
0026     delete[] m_boxes;
0027 }
0028 
0029 void PointFinder::append(QPointF point) {
0030     int nx = point.x() / m_radius;
0031     int ny = point.y() / m_radius;
0032     m_points.append(point);
0033     if (nx >= 0 && ny >= 0 && nx < m_xbins && ny < m_ybins) {
0034         m_boxes[nx][ny].append(point);
0035     }
0036 }
0037 
0038 QList<QPointF> PointFinder::points() {
0039     return m_points;
0040 }
0041 
0042 QList<QPointF> PointFinder::find_neighbours(QPointF point) {
0043     QList<QPointF> result;
0044     int nx = point.x() / m_radius;
0045     int ny = point.y() / m_radius;
0046     for (int nnx=nx-1; nnx <= nx+1; nnx++) {
0047         if (nnx < 0 || nnx >= m_xbins) continue;
0048         for (int nny = ny-1; nny <= ny+1; nny++) {
0049             if (nny < 0 || nny >= m_ybins) continue;
0050             for (int i=0; i<m_boxes[nnx][nny].size(); i++) {
0051                 QPointF other = m_boxes[nnx][nny][i];
0052                 if (QLineF(point, other).length() < m_radius &&
0053                             point != other) {
0054                     result.append(other);
0055                 }
0056             }
0057         }
0058     }
0059     return result;
0060 }