KPublicTransport

convexhull.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "convexhull_p.h"
7
8#include <QDebug>
9#include <QLineF>
10#include <QPolygonF>
11
12using namespace KPublicTransport;
13
14static double orientation(QPointF p, QPointF q, QPointF r)
15{
16 return (q.y() - p.y()) * (r.x() - q.x()) - (r.y() - q.y()) * (q.x() - p.x());
17}
18
19// see https://en.wikipedia.org/wiki/Gift_wrapping_algorithm
20QPolygonF ConvexHull::compute(const std::vector<QPointF> &points)
21{
22 QPolygonF hull;
23 if (points.size() < 3) {
24 return hull;
25 }
26
27 // find left-most point
28 const auto it = std::min_element(points.begin(), points.end(), [](auto lhs, auto rhs) {
29 return lhs.x() < rhs.x();
30 });
31 hull.push_back(*it);
32 auto p = std::distance(points.begin(), it);
33
34 while (true) {
35 auto q = (p + 1) % points.size();
36 for (std::size_t r = 0; r < points.size(); ++r) {
37 if (q == r) {
38 continue;
39 }
40
41 auto o = orientation(points[p], points[q], points[r]);
42 if (o < 0.0) {
43 q = r;
44 } else if (o == 0.0) {
45 if (QLineF(points[p], points[r]).length() > QLineF(points[p], points[q]).length()) {
46 q = r;
47 }
48 }
49 }
50 p = q;
51 hull.push_back(points[p]);
52
53 if (hull.isClosed()) {
54 break;
55 }
56 }
57
58 return hull;
59}
Query operations and data types for accessing realtime public transport information from online servi...
void push_back(parameter_type value)
qreal x() const const
qreal y() const const
bool isClosed() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:10:39 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.