File indexing completed on 2024-05-12 03:50:41

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2010 Dennis Nienhüser <nienhueser@kde.org>
0004 //
0005 
0006 #include "RoutingPoint.h"
0007 
0008 #include <QTextStream>
0009 
0010 #include <cmath>
0011 
0012 namespace Marble
0013 {
0014 
0015 RoutingPoint::RoutingPoint( qreal lon, qreal lat ) :
0016         m_lon( lon), m_lonRad( lon * M_PI / 180.0 ),
0017         m_lat( lat ), m_latRad( lat * M_PI / 180.0 )
0018 {
0019     // nothing to do
0020 }
0021 
0022 qreal RoutingPoint::lon() const
0023 {
0024     return m_lon;
0025 }
0026 
0027 qreal RoutingPoint::lat() const
0028 {
0029     return m_lat;
0030 }
0031 
0032 // Code based on https://www.movable-type.co.uk/scripts/latlong.html
0033 qreal RoutingPoint::bearing( const RoutingPoint &other ) const
0034 {
0035     qreal deltaLon = other.m_lonRad - m_lonRad;
0036     qreal y = sin( deltaLon ) * cos( other.m_latRad );
0037     qreal x = cos( m_latRad ) * sin( other.m_latRad ) -
0038               sin( m_latRad ) * cos( other.m_latRad ) * cos( deltaLon );
0039     return atan2( y, x );
0040 }
0041 
0042 // From MarbleMath::distanceSphere
0043 qreal RoutingPoint::distance( const RoutingPoint &other ) const
0044 {
0045     qreal h1 = sin( 0.5 * ( other.m_latRad - m_latRad ) );
0046     qreal h2 = sin( 0.5 * ( other.m_lonRad - m_lonRad ) );
0047     qreal d = h1 * h1 + cos( m_latRad ) * cos( other.m_latRad ) * h2 * h2;
0048 
0049     return 6378137.0 * 2.0 * atan2( sqrt( d ), sqrt( 1.0 - d ) );
0050 }
0051 
0052 QTextStream& operator<<( QTextStream& stream, const RoutingPoint &p )
0053 {
0054     stream << "(" << p.lon() << ", " << p.lat() << ")";
0055     return stream;
0056 }
0057 
0058 } // namespace Marble