Marble

RoutingPoint.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2010 Dennis Nienhüser <nienhueser@kde.org>
4//
5
6#include "RoutingPoint.h"
7
8#include <QTextStream>
9
10#include <cmath>
11
12namespace Marble
13{
14
15RoutingPoint::RoutingPoint(qreal lon, qreal lat)
16 : m_lon(lon)
17 , m_lonRad(lon * M_PI / 180.0)
18 , m_lat(lat)
19 , m_latRad(lat * M_PI / 180.0)
20{
21 // nothing to do
22}
23
24qreal RoutingPoint::lon() const
25{
26 return m_lon;
27}
28
29qreal RoutingPoint::lat() const
30{
31 return m_lat;
32}
33
34// Code based on https://www.movable-type.co.uk/scripts/latlong.html
35qreal RoutingPoint::bearing(const RoutingPoint &other) const
36{
37 qreal deltaLon = other.m_lonRad - m_lonRad;
38 qreal y = sin(deltaLon) * cos(other.m_latRad);
39 qreal x = cos(m_latRad) * sin(other.m_latRad) - sin(m_latRad) * cos(other.m_latRad) * cos(deltaLon);
40 return atan2(y, x);
41}
42
43// From MarbleMath::distanceSphere
44qreal RoutingPoint::distance(const RoutingPoint &other) const
45{
46 qreal h1 = sin(0.5 * (other.m_latRad - m_latRad));
47 qreal h2 = sin(0.5 * (other.m_lonRad - m_lonRad));
48 qreal d = h1 * h1 + cos(m_latRad) * cos(other.m_latRad) * h2 * h2;
49
50 return 6378137.0 * 2.0 * atan2(sqrt(d), sqrt(1.0 - d));
51}
52
53QTextStream &operator<<(QTextStream &stream, const RoutingPoint &p)
54{
55 stream << "(" << p.lon() << ", " << p.lat() << ")";
56 return stream;
57}
58
59} // namespace Marble
There are many Point classes, but this is mine.
qreal distance(const RoutingPoint &other) const
Calculates the distance in meter between this point and the given other point.
qreal bearing(const RoutingPoint &other) const
Calculates the bearing of the line defined by this point and the given other point.
qreal lon() const
Longitude of the point.
qreal lat() const
Latitude of the point.
Binds a QML item to a specific geodetic location in screen coordinates.
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:52:10 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.