Debian Codegraph @

src/coord.rs

raw symbols SCIP
1 //! Coordinate handling
2 //!
3 //! This module contains one type, [`Coord`]. That has methods to convert two and from several
4 //! different coordinate systems. Mainly:
5 //! - Equatorial (Hour Angle, Declination)
6 //! - Horizon (Azimuth, Altitude)
7 //! - Ecliptic (Beta, Lambda)
8 //!
9 //! This type also contains algorithms for converting from Cartesian (rectangular) coordinates, rise and set times, distance between angles, etc.
10
11 use crate::time::*;
12
13 /// Gets the mean obliquity of the ecliptic at a certain date
14 pub fn mean_obliquity_ecl(d: Date) -> Angle {
15 let t = d.centuries();
16 Angle::from_degrees(
17 23.439_292 - ((46.815 * t + 0.0006 * (t * t) - 0.00181 * (t * t * t)) / 3600.0),
18 )
19 }
20
21 /**
22 Pair of angles, Representing "How far up" and "How far round"
23
24 | Property | Latitude | Longitude | Depends On | To Method | From Method |
25 |-------------------|-------------------|---------------------|---------------------------------|------------------------|-----------------------------|
26 | Equatorial | Declination (δ) | Right Ascension (α) | | [`Coord::equatorial()`]| [`Coord::from_equatorial()`]|
27 | Horizontal | Altitude (a) | Azimuth (A) | Date, Time, Latitude, Longitude | [`Coord::horizon()`] | [`Coord::from_horizon()`] |
28 | Ecliptic | Ecl. Latitude (β) | Ecl. Longitude (λ) | Date[^1] | [`Coord::ecliptic()`] | [`Coord::from_ecliptic()`] |
29 | Cartesian | N/A (3D system) | N/A (3D system) | Distance | [`Coord::cartesian()`] | [`Coord::from_cartesian()`] |
30
31 Additional Methods:
32 * Distance between coordinates: [`Coord::dist()`]
33 * Rise and set times of a coordinate in the sky [`Coord::riseset()`]
34 * Precession [`Coord::precess()`]
35
36 [^1]: The plane of the ecliptic varies slightly with perturbations in the orbit and inclination of the earth.
37 */
38 #[derive(Debug, PartialEq, Clone, Copy, Default)]
39 pub struct Coord(Angle, Angle);
40 impl Coord {
41 /// Right Ascension and Declination
42 pub const fn equatorial(self) -> (Angle, Angle) {
43 (self.0, self.1)
44 }
45 /// Right Ascension and Declination
46 pub const fn from_equatorial(x: Angle, y: Angle) -> Self {
47 Coord(x, y)
48 }
49
50 /// Azimuth and Altitude, dependent on location and time
51 ///
52 /// From Practical Astronomy with Your Calculator, Although similar algorithms exist in other sources
53 pub fn horizon(self, date: Date, lati: Angle, longi: Angle) -> (Angle, Angle) {
54 let (ra, de) = self.equatorial();
55 let ha = date.time().gst(date) + longi - ra;
56 let alt = Angle::asin(de.sin() * lati.sin() + de.cos() * lati.cos() * ha.cos());
57 let azip = Angle::acos((de.sin() - lati.sin() * alt.sin()) / (lati.cos() * alt.cos()));
58 let azi = match ha.sin() < 0.0 {
59 true => azip,
60 false => Angle::from_degrees(360.0 - azip.degrees()),
61 };
62 (azi, alt)
63 }
64 /// Azimuth and Altitude, dependent on location, and time
65 ///
66 /// From Practical Astronomy with Your Calculator, Although similar algorithms exist in other sources
67 pub fn from_horizon(azi: Angle, alt: Angle, date: Date, lati: Angle, longi: Angle) -> Self {
68 let de = Angle::asin(alt.sin() * lati.sin() + alt.cos() * lati.cos() * azi.cos());
69 let hap = Angle::acos((alt.sin() - lati.sin() * de.sin()) / (lati.cos() * de.cos()));
70 let ha = match azi.sin() < 0.0 {
71 true => hap,
72 false => Angle::from_degrees(360.0 - hap.degrees()),
73 };
74 Coord::from_equatorial(date.time().gst(date) + longi - ha, de)
75 }
76
77 /// Used in solar calculations, based on the plane of the orbit of the earth
78 ///
79 /// From Practical Astronomy with Your Calculator, Although similar algorithms exist in other sources
80 pub fn ecliptic(self, d: Date) -> (Angle, Angle) {
81 let (ra, de) = self.equatorial();
82 let e = mean_obliquity_ecl(d);
83 let beta = Angle::asin(de.sin() * e.cos() - de.cos() * e.sin() * ra.sin());
84 let y = ra.sin() * e.cos() + de.tan() * e.sin();
85 let x = ra.cos();
86 let lambda = Angle::atan2(y, x);
87 (lambda, beta)
88 }
89 /// Used in solar calculations, based on the plane of the orbit of the earth
90 ///
91 /// From Practical Astronomy with Your Calculator, Although similar algorithms exist in other sources
92 pub fn from_ecliptic(lambda: Angle, beta: Angle, d: Date) -> Self {
93 let e = mean_obliquity_ecl(d);
94 let de = Angle::asin(beta.sin() * e.cos() + beta.cos() * e.sin() * lambda.sin());
95 let ra = Angle::atan2(lambda.sin() * e.cos() - beta.tan() * e.sin(), lambda.cos());
96 Coord::from_equatorial(ra, de)
97 }
98
99 /// Convert 3D Rectangular Coordinates to 2D Polar Coordinates
100 ///
101 /// This does not retain the distance to the object
102 pub fn from_cartesian(x: f64, y: f64, z: f64) -> Self {
103 let (tx, ty, tz) = (x, y, z);
104 let r = (tx * tx + ty * ty + tz * tz).sqrt();
105 let l = Angle::atan2(ty, tx);
106 let t2 = Angle::from_radians(0.5 * std::f64::consts::PI - (tz / r).acos());
107
108 Coord::from_equatorial(l, t2)
109 }
110
111 /// Convert 2D Polar into 3D rectangular, depends on distance
112 pub fn cartesian(self, dist: f64) -> (f64, f64, f64) {
113 let (lat, long) = self.equatorial();
114 let x = dist * lat.cos() * long.cos();
115 let y = dist * lat.cos() * long.sin();
116 let z = dist * lat.sin();
117
118 (x, y, z)
119 }
120
121 /// Returns the angle between two objects
122 pub fn dist(self, from: Self) -> Angle {
123 let ((a1, d1), (a2, d2)) = (self.equatorial(), from.equatorial());
124 Angle::acos(d1.sin() * d2.sin() + d1.cos() * d2.cos() * (a1 - a2).cos())
125 }
126 /// Returns (Rise, Set) UT, This function will fail for locations in the sky that never appear over the horizon
127 ///
128 /// From Practical Astronomy with Your Calculator, Although similar algorithms exist in other sources
129 pub fn riseset(self, date: Date, lati: Angle, longi: Angle) -> Option<(Angle, Angle)> {
130 let (ra, de) = self.equatorial();
131 let ar = Angle::acos(de.sin() / lati.cos());
132 let h = Angle::acos(-lati.tan() * de.tan());
133 if h.radians().is_nan() || ar.radians().is_nan() {
134 return None;
135 }
136 let lsts = (ra - h - longi).ungst(date);
137 let lstr = (ra + h - longi).ungst(date);
138 Some((lsts, lstr))
139 }
140
141 /// (Roughly) Accounts for precession in coordinates.
142 pub fn precess(self, epoch: Date, d: Date) -> Self {
143 let (ra, de) = self.equatorial();
144 let diff = (d.julian() - epoch.julian()) / 365.25;
145 let m =
146 Angle::from_clock(0, 0, 3.07234) + Angle::from_clock(0, 0, 0.00186) * epoch.centuries();
147 let n = Angle::from_degminsec(0, 0, 20.0468)
148 + Angle::from_degminsec(0, 0, 0.0085) * epoch.centuries();
149 let deltara = m.degrees() + n.degrees() * ra.sin() * de.tan();
150 let deltade = n.degrees() * ra.cos();
151 Coord::from_equatorial(
152 ra + Angle::from_degrees(deltara * diff),
153 de + Angle::from_degrees(deltade * diff),
154 )
155 }
156 }
157
158 #[cfg(test)]
159 mod tests {
160 use super::*;
161
162 // Many of these tests do not conform with data you can pull out of stellarium/other tools, they are correct nonetheless.
163 // * How do you know?: By personal confirmation of the result data with other resources
164 // * Then how can I use these functions?: In conjunction with the functions for correction (procession, nutation, abberation, refraction)
165 // Note that even without correction, these tests are almost always within 16' (half the moons diameter).
166 #[test]
167 fn test_horiz() {
168 let arcturus = Coord::from_equatorial(
169 Angle::from_clock(14, 16, 50.0),
170 Angle::from_degminsec(19, 02, 50.1),
171 );
172 let sirius = Coord::from_equatorial(
173 Angle::from_clock(6, 46, 13.1),
174 Angle::from_degminsec(-16, 45, 06.8),
175 );
176 assert_eq!(
177 arcturus.horizon(
178 Date::from_calendar(2025, 3, 10, Angle::from_clock(19, 52, 25.0)),
179 Angle::from_degrees(55.47885),
180 Angle::from_degrees(133.94531)
181 ),
182 (
183 Angle::from_degminsec(220, 39, 16.2),
184 Angle::from_degminsec(48, 6, 46.1)
185 )
186 );
187 assert_eq!(
188 sirius.horizon(
189 Date::from_calendar(2025, 3, 7, Angle::from_clock(23, 36, 52.0)),
190 Angle::from_degrees(5.0),
191 Angle::from_degrees(-1.0)
192 ),
193 (
194 Angle::from_degminsec(249, 37, 18.2),
195 Angle::from_degminsec(28, 34, 54.8)
196 )
197 );
198 assert_eq!(
199 sirius.horizon(
200 Date::from_calendar(2025, 3, 11, Angle::from_clock(2, 0, 0.0)),
201 Angle::from_degrees(44.8714),
202 Angle::from_degrees(-93.20801)
203 ),
204 (
205 Angle::from_degminsec(184, 47, 2.3),
206 Angle::from_degminsec(29, 45, 27.2)
207 )
208 );
209 assert_eq!(
210 Coord::from_horizon(
211 Angle::from_degminsec(184, 47, 2.3),
212 Angle::from_degminsec(29, 45, 27.2),
213 Date::from_calendar(2025, 3, 11, Angle::from_clock(2, 0, 0.0)),
214 Angle::from_degrees(44.8714),
215 Angle::from_degrees(-93.20801)
216 ),
217 sirius
218 );
219 assert_eq!(sirius.dist(arcturus), Angle::from_degminsec(115, 55, 5.17));
220 }
221
222 #[test]
223 fn test_riseset() {
224 let c = Coord::from_equatorial(
225 Angle::from_clock(23, 39, 20.0),
226 Angle::from_degminsec(21, 42, 00.0),
227 );
228 assert_eq!(
229 c.riseset(
230 Date::from_calendar(1980, 8, 24, Angle::default()),
231 Angle::from_degrees(30.0),
232 Angle::from_degrees(64.0)
233 ),
234 Some((Angle::from_clock(14, 18, 9.0), Angle::from_clock(4, 6, 5.0)))
235 );
236 assert_eq!(
237 c.riseset(
238 Date::from_calendar(1980, 8, 24, Angle::default()),
239 Angle::from_degrees(-85.0),
240 Angle::from_degrees(0.0),
241 ),
242 None
243 );
244 }
245
246 #[test]
247 fn test_ecliptic() {
248 let star1 = Coord::from_equatorial(
249 Angle::from_clock(9, 34, 53.6),
250 Angle::from_degminsec(19, 32, 14.2),
251 );
252 assert_eq!(
253 star1.ecliptic(Date::from_calendar(1950, 0, 1, Angle::default())),
254 (
255 Angle::from_degminsec(139, 41, 10.0),
256 Angle::from_degminsec(4, 52, 31.0)
257 )
258 );
259 assert_eq!(
260 Coord::from_ecliptic(
261 Angle::from_degminsec(139, 41, 10.0),
262 Angle::from_degminsec(4, 52, 31.0),
263 Date::from_calendar(1950, 0, 1, Angle::default())
264 ),
265 star1
266 );
267 }
268 }