UNPKG

5.33 kBJavaScriptView Raw
1/**
2 * @copyright 2013 Sonia Keys
3 * @copyright 2016 commenthol
4 * @license MIT
5 * @module line
6 */
7/**
8 * Line: Chapter 19, Bodies in Straight Line
9 */
10
11import base from './base';
12import interp from './interpolation';
13
14/**
15 * Time computes the time at which a moving body is on a straight line (great
16 * circle) between two fixed points, such as stars.
17 *
18 * Coordinates may be right ascensions and declinations or longitudes and
19 * latitudes. Fixed points are r1, d1, r2, d2. Moving body is an ephemeris
20 * of 5 rows, r3, d3, starting at time t1 and ending at time t5. Time scale
21 * is arbitrary.
22 *
23 * @throws Error
24 * @param {Number} r1 - right ascension Coordinate 1
25 * @param {Number} d1 - declination Coordinate 1
26 * @param {Number} r2 - right ascension Coordinate 2
27 * @param {Number} d2 - declination Coordinate 2
28 * @param {Number} r3 - right ascension Coordinate 3
29 * @param {Number} d2 - declination Coordinate 3
30 * @param {Array} d3 -
31 * @param {Array} t1 - time in Julian Days
32 * @param {Array} t5 - time in Julian Days
33 * @returns {Number} time of alignment in Julian Days
34 */
35export function time(r1, d1, r2, d2, r3, d3, t1, t5) {
36 // (r1, d1, r2, d2 float64, r3, d3 []float64, t1, t5 float64) (float64, error)
37 if (r3.length !== 5 || d3.length !== 5) {
38 throw new Error('r3, d3 must be length 5');
39 }
40 var gc = new Array(5);
41 r3.forEach(function (r3i, i) {
42 // (19.1) p. 121
43 gc[i] = Math.tan(d1) * Math.sin(r2 - r3i) + Math.tan(d2) * Math.sin(r3i - r1) + Math.tan(d3[i]) * Math.sin(r1 - r2);
44 });
45 var l5 = new interp.Len5(t1, t5, gc);
46 return l5.zero(false);
47}
48
49/**
50 * Angle returns the angle between great circles defined by three points.
51 *
52 * Coordinates may be right ascensions and declinations or longitudes and
53 * latitudes. If r1, d1, r2, d2 defines one line and r2, d2, r3, d3 defines
54 * another, the result is the angle between the two lines.
55 *
56 * Algorithm by Meeus.
57 */
58export function angle(r1, d1, r2, d2, r3, d3) {
59 // (r1, d1, r2, d2, r3, d3 float64) float64
60 var _base$sincos = base.sincos(d2),
61 sd2 = _base$sincos[0],
62 cd2 = _base$sincos[1];
63
64 var _base$sincos2 = base.sincos(r2 - r1),
65 sr21 = _base$sincos2[0],
66 cr21 = _base$sincos2[1];
67
68 var _base$sincos3 = base.sincos(r3 - r2),
69 sr32 = _base$sincos3[0],
70 cr32 = _base$sincos3[1];
71
72 var C1 = Math.atan2(sr21, cd2 * Math.tan(d1) - sd2 * cr21);
73 var C2 = Math.atan2(sr32, cd2 * Math.tan(d3) - sd2 * cr32);
74 return C1 + C2;
75}
76
77/**
78 * Error returns an error angle of three nearly co-linear points.
79 *
80 * For the line defined by r1, d1, r2, d2, the result is the anglular distance
81 * between that line and r0, d0.
82 *
83 * Algorithm by Meeus.
84 */
85export function error(r1, d1, r2, d2, r0, d0) {
86 // (r1, d1, r2, d2, r0, d0 float64) float64
87 var _base$sincos4 = base.sincos(r1),
88 sr1 = _base$sincos4[0],
89 cr1 = _base$sincos4[1];
90
91 var _base$sincos5 = base.sincos(d1),
92 sd1 = _base$sincos5[0],
93 cd1 = _base$sincos5[1];
94
95 var _base$sincos6 = base.sincos(r2),
96 sr2 = _base$sincos6[0],
97 cr2 = _base$sincos6[1];
98
99 var _base$sincos7 = base.sincos(d2),
100 sd2 = _base$sincos7[0],
101 cd2 = _base$sincos7[1];
102
103 var X1 = cd1 * cr1;
104 var X2 = cd2 * cr2;
105 var Y1 = cd1 * sr1;
106 var Y2 = cd2 * sr2;
107 var Z1 = sd1;
108 var Z2 = sd2;
109 var A = Y1 * Z2 - Z1 * Y2;
110 var B = Z1 * X2 - X1 * Z2;
111 var C = X1 * Y2 - Y1 * X2;
112 var m = Math.tan(r0);
113 var n = Math.tan(d0) / Math.cos(r0);
114 return Math.asin((A + B * m + C * n) / (Math.sqrt(A * A + B * B + C * C) * Math.sqrt(1 + m * m + n * n)));
115}
116
117/**
118 * AngleError returns both an angle as in the function Angle, and an error
119 * as in the function Error.
120 *
121 * The algorithm is by B. Pessens.
122 *
123 * @returns {Number[]} [ψ, ω]
124 * {Number} ψ - angle between great circles defined by three points.
125 * {Number} ω - error angle of three nearly co-linear points
126 */
127export function angleError(r1, d1, r2, d2, r3, d3) {
128 var _base$sincos8 = base.sincos(r1),
129 sr1 = _base$sincos8[0],
130 cr1 = _base$sincos8[1];
131
132 var _base$sincos9 = base.sincos(d1),
133 c1 = _base$sincos9[0],
134 cd1 = _base$sincos9[1];
135
136 var _base$sincos10 = base.sincos(r2),
137 sr2 = _base$sincos10[0],
138 cr2 = _base$sincos10[1];
139
140 var _base$sincos11 = base.sincos(d2),
141 c2 = _base$sincos11[0],
142 cd2 = _base$sincos11[1];
143
144 var _base$sincos12 = base.sincos(r3),
145 sr3 = _base$sincos12[0],
146 cr3 = _base$sincos12[1];
147
148 var _base$sincos13 = base.sincos(d3),
149 c3 = _base$sincos13[0],
150 cd3 = _base$sincos13[1];
151
152 var a1 = cd1 * cr1;
153 var a2 = cd2 * cr2;
154 var a3 = cd3 * cr3;
155 var b1 = cd1 * sr1;
156 var b2 = cd2 * sr2;
157 var b3 = cd3 * sr3;
158 var l1 = b1 * c2 - b2 * c1;
159 var l2 = b2 * c3 - b3 * c2;
160 var l3 = b1 * c3 - b3 * c1;
161 var m1 = c1 * a2 - c2 * a1;
162 var m2 = c2 * a3 - c3 * a2;
163 var m3 = c1 * a3 - c3 * a1;
164 var n1 = a1 * b2 - a2 * b1;
165 var n2 = a2 * b3 - a3 * b2;
166 var n3 = a1 * b3 - a3 * b1;
167 var ψ = Math.acos((l1 * l2 + m1 * m2 + n1 * n2) / (Math.sqrt(l1 * l1 + m1 * m1 + n1 * n1) * Math.sqrt(l2 * l2 + m2 * m2 + n2 * n2)));
168 var ω = Math.asin((a2 * l3 + b2 * m3 + c2 * n3) / (Math.sqrt(a2 * a2 + b2 * b2 + c2 * c2) * Math.sqrt(l3 * l3 + m3 * m3 + n3 * n3)));
169 return [ψ, ω];
170}
171
172export default {
173 time: time,
174 angle: angle,
175 error: error,
176 angleError: angleError
177};
\No newline at end of file