I would use QPoint and QPoint::manhattanLength() to calculate the distance.
int closestPoint
(const QList<QPoint>
&points,
const QPoint &pt
) { if(points.isEmpty()) return -1;
int minIdx = 0;
int minDistance = (points.at(0)-pt).manhattanLength();
for(int i=1;i<points.size();++i) {
int dist = (points.at(i)-pt).manhattanLength();
if(dist < minDistance) { minIdx = i; minDistance = dist; }
}
// cutoff: if(minDistance > 10) return -1;
return minIdx;
}
int closestPoint(const QList<QPoint> &points, const QPoint &pt) {
if(points.isEmpty()) return -1;
int minIdx = 0;
int minDistance = (points.at(0)-pt).manhattanLength();
for(int i=1;i<points.size();++i) {
int dist = (points.at(i)-pt).manhattanLength();
if(dist < minDistance) { minIdx = i; minDistance = dist; }
}
// cutoff: if(minDistance > 10) return -1;
return minIdx;
}
To copy to clipboard, switch view to plain text mode
And then:
QList<QPoint> points;
int closest = closestPoint(points, clickedPoint);
if(closest >=0) points.removeAt(closest);
QList<QPoint> points;
QPoint clickedPoint;
int closest = closestPoint(points, clickedPoint);
if(closest >=0) points.removeAt(closest);
To copy to clipboard, switch view to plain text mode
You'll probably want some cutoff on the distance not to remove points that are quite far away from the destination but are still closer than all other points.
Bookmarks