# Marble

RoutingPoint.cpp
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2010 Dennis NienhÃ¼ser <[email protected]>
9 //
10
11 #include "RoutingPoint.h"
12
13 #include <QTextStream>
14
15 #include <cmath>
16
17 namespace Marble
18 {
19
20 RoutingPoint::RoutingPoint( qreal lon, qreal lat ) :
21  m_lon( lon), m_lonRad( lon * M_PI / 180.0 ),
22  m_lat( lat ), m_latRad( lat * M_PI / 180.0 )
23 {
24  // nothing to do
25 }
26
27 qreal RoutingPoint::lon() const
28 {
29  return m_lon;
30 }
31
32 qreal RoutingPoint::lat() const
33 {
34  return m_lat;
35 }
36
37 // Code based on https://www.movable-type.co.uk/scripts/latlong.html
38 qreal RoutingPoint::bearing( const RoutingPoint &other ) const
39 {
41  qreal y = sin( deltaLon ) * cos( other.m_latRad );
42  qreal x = cos( m_latRad ) * sin( other.m_latRad ) -
43  sin( m_latRad ) * cos( other.m_latRad ) * cos( deltaLon );
44  return atan2( y, x );
45 }
46
47 // From MarbleMath::distanceSphere
48 qreal RoutingPoint::distance( const RoutingPoint &other ) const
49 {
50  qreal h1 = sin( 0.5 * ( other.m_latRad - m_latRad ) );
51  qreal h2 = sin( 0.5 * ( other.m_lonRad - m_lonRad ) );
52  qreal d = h1 * h1 + cos( m_latRad ) * cos( other.m_latRad ) * h2 * h2;
53
54  return 6378137.0 * 2.0 * atan2( sqrt( d ), sqrt( 1.0 - d ) );
55 }
56
57 QTextStream& operator<<( QTextStream& stream, const RoutingPoint &p )
58 {
59  stream << "(" << p.lon() << ", " << p.lat() << ")";
60  return stream;
61 }
62
63 } // namespace Marble
qreal lat() const
Latitude of the point.
qreal lon() const
Longitude of the point.
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.
QDataStream & operator<<(QDataStream &out, const KDateTime::Spec &spec)
There are many Point classes, but this is mine.
Definition: RoutingPoint.h:26
qreal bearing(const RoutingPoint &other) const
Calculates the bearing of the line defined by this point and the given other point.
This file is part of the KDE documentation.