Marble

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

KDE's Doxygen guidelines are available online.