Marble

MarbleMath.h
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2007 Torsten Rahn <tackat@kde.org>
4// SPDX-FileCopyrightText: 2007 Inge Wallin <ingwa@kde.org>
5//
6
7#ifndef MARBLE_MARBLEMATH_H
8#define MARBLE_MARBLEMATH_H
9
10#include <QtGlobal>
11
12#include <cmath>
13
14namespace
15{
16const qreal a1 = 1.0 / 6.0;
17const qreal a2 = 1.0 / 24.0;
18const qreal a3 = 61.0 / 5040;
19const qreal a4 = 277.0 / 72576.0;
20const qreal a5 = 50521.0 / 39916800.0;
21const qreal a6 = 41581.0 / 95800320.0;
22const qreal a7 = 199360981.0 / 1307674368000.0;
23const qreal a8 = 228135437.0 / 4184557977600.0;
24const qreal a9 = 2404879675441.0 / 121645100408832000.0;
25const qreal a10 = 14814847529501.0 / 2043637686868377600.0;
26const qreal a11 = 69348874393137901.0 / 25852016738884976640000.0;
27const qreal a12 = 238685140977801337.0 / 238634000666630553600000.0;
28const qreal a13 = 4087072509293123892361.0 / 10888869450418352160768000000.0;
29const qreal a14 = 454540704683713199807.0 / 3209350995912777478963200000.0;
30const qreal a15 = 441543893249023104553682821.0 / 8222838654177922817725562880000000.0;
31const qreal a16 = 2088463430347521052196056349.0 / 102156677868375135241390522368000000.0;
32}
33
34namespace Marble
35{
36
37/**
38 * @brief This method calculates the shortest distance between two points on a sphere.
39 * @brief See: https://en.wikipedia.org/wiki/Great-circle_distance
40 * @param lon1 longitude of first point in radians
41 * @param lat1 latitude of first point in radians
42 * @param lon2 longitude of second point in radians
43 * @param lat2 latitude of second point in radians
44 */
45inline qreal distanceSphere(qreal lon1, qreal lat1, qreal lon2, qreal lat2)
46{
47 qreal h1 = sin(0.5 * (lat2 - lat1));
48 qreal h2 = sin(0.5 * (lon2 - lon1));
49 qreal d = h1 * h1 + cos(lat1) * cos(lat2) * h2 * h2;
50
51 return 2.0 * atan2(sqrt(d), sqrt(1.0 - d));
52}
53
54/**
55 * @brief This method roughly calculates the shortest distance between two points on a sphere.
56 * @brief It's probably faster than distanceSphere(...) but for 7 significant digits only has
57 * @brief an accuracy of about 1 arcmin.
58 * @brief See: https://en.wikipedia.org/wiki/Great-circle_distance
59 */
60inline qreal distanceSphereApprox(qreal lon1, qreal lat1, qreal lon2, qreal lat2)
61{
62 return acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon1 - lon2));
63}
64
65/**
66 * @brief This method is a fast Mac Laurin power series approximation of the
67 * @brief inverse Gudermannian. The inverse Gudermannian gives the vertical
68 * @brief position y in the Mercator projection in terms of the latitude.
69 * @brief See: https://en.wikipedia.org/wiki/Mercator_projection
70 */
71inline qreal gdInv(qreal x)
72{
73 const qreal x2 = x * x;
74 return x
75 + x * x2
76 * (a1
77 + x2
78 * (a2
79 + x2
80 * (a3
81 + x2
82 * (a4
83 + x2
84 * (a5
85 + x2
86 * (a6
87 + x2
88 * (a7
89 + x2
90 * (a8
91 + x2
92 * (a9
93 + x2
94 * (a10
95 + x2
96 * (a11
97 + x2 * (a12 + x2 * (a13 + x2 * (a14 + x2 * (a15 + x2 * (a16))))))))))))))));
98}
99
100}
101
102/**
103 * @brief This method is a fast Mac Laurin power series approximation of the
104 * Gudermannian. The Gudermannian gives the latitude
105 * in the Mercator projection in terms of the vertical position y.
106 * See: https://en.wikipedia.org/wiki/Mercator_projection
107 */
108inline qreal gd(qreal x)
109{
110 /*
111 const qreal x2 = x * x;
112 return x
113 - x * x2 * ( a1
114 - x2 * ( a2 - x2 * ( a3 - x2 * ( a4 - x2 * ( a5
115 - x2 * ( a6 - x2 * ( a7 - x2 * ( a8 - x2 * ( a9
116 - x2 * ( a10 - x2 * ( a11 - x2 * ( a12 - x2 * ( a13
117 - x2 * ( a14 - x2 * ( a15 - x2 * ( a16 ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) );
118 */
119
120 return atan(sinh(x));
121}
122
123#endif
Binds a QML item to a specific geodetic location in screen coordinates.
qreal distanceSphereApprox(qreal lon1, qreal lat1, qreal lon2, qreal lat2)
This method roughly calculates the shortest distance between two points on a sphere.
Definition MarbleMath.h:60
qreal gdInv(qreal x)
This method is a fast Mac Laurin power series approximation of the.
Definition MarbleMath.h:71
qreal distanceSphere(qreal lon1, qreal lat1, qreal lon2, qreal lat2)
This method calculates the shortest distance between two points on a sphere.
Definition MarbleMath.h:45
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.