-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathaspect.ts
More file actions
252 lines (220 loc) · 7.59 KB
/
Copy pathaspect.ts
File metadata and controls
252 lines (220 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import type { Points } from './radix'
import type { AspectData, Settings } from './settings'
import { radiansToDegree } from './utils'
export interface FormedAspect {
point: {
name: string
position: number
}
toPoint: {
name: string
position: number
}
aspect: {
name: string
degree: number
color: string
width: number
orbit: number
}
precision: string
}
const DEFAULT_ASPECTS = {
conjunction: { degree: 0, orbit: 10, color: 'transparent', width: 4 },
square: { degree: 90, orbit: 8, color: '#FF4500', width: 4 },
trine: { degree: 120, orbit: 8, color: '#27AE60', width: 4 },
opposition: { degree: 180, orbit: 10, color: '#27AE60', width: 4 }
}
/**
* Aspects calculator
*
* @class
* @public
* @constructor
* @param {AspectPoints} points; {"Sun":[0], "Moon":[90], "Neptune":[120], "As":[30]}
* @param {Object | null } settings
*/
class AspectCalculator {
settings: Partial<Settings>
toPoints: Points
context: this
constructor (toPoints: Points, settings?: Partial<Settings>) {
if (toPoints == null) {
throw new Error('Param \'toPoint\' must not be empty.')
}
this.settings = settings ?? {}
this.settings.ASPECTS = settings?.ASPECTS ?? DEFAULT_ASPECTS
this.toPoints = toPoints
this.context = this
}
/**
* Getter for this.toPoints
* @see constructor
*
* @return {Object}
*/
getToPoints (): Points {
return this.toPoints
}
/**
* Radix aspects
*
* In radix calculation is the param "points" the same as param "toPoints" in constructor
* , but without special points such as: As,Ds, Mc, Ic, ...
*
* @param {Object} points; {"Sun":[0], "Moon":[90]}
*
* @return {Array<Object>} [{"aspect":{"name":"conjunction", "degree":120}"", "point":{"name":"Sun", "position":123}, "toPoint":{"name":"Moon", "position":345}, "precision":0.5}]]
*/
radix (points: Points): FormedAspect[] {
if (points == null) {
return []
}
const aspects: FormedAspect[] = []
for (const point in points) {
if (points.hasOwnProperty(point)) {
for (const toPoint in this.toPoints) {
if (this.toPoints.hasOwnProperty(toPoint)) {
if (point !== toPoint) {
for (const aspect in this.settings.ASPECTS) {
if (this.hasAspect(points[point][0], this.toPoints[toPoint][0], this.settings.ASPECTS[aspect])) {
aspects.push(
{
aspect: { name: aspect, degree: this.settings.ASPECTS[aspect].degree, orbit: this.settings.ASPECTS[aspect].orbit, color: this.settings.ASPECTS[aspect].color, width: this.settings.ASPECTS[aspect].width },
point: { name: point, position: points[point][0] },
toPoint: { name: toPoint, position: this.toPoints[toPoint][0] },
precision: this.calcPrecision(points[point][0], this.toPoints[toPoint][0], this.settings.ASPECTS[aspect].degree).toFixed(4)
}
)
}
}
}
}
}
}
}
return aspects.sort(this.compareAspectsByPrecision)
}
/**
* Transit aspects
*
* @param {Object} points - transiting points; {"Sun":[0, 1], "Uranus":[90, -1], "NAME":[ANGLE, SPEED]};
* @return {Array<Object>} [{"aspect":{"name":"conjunction", "degree":120}"", "point":{"name":"Sun", "position":123}, "toPoint":{"name":"Moon", "position":345}, "precision":0.5}]]
*/
transit (points: Points): FormedAspect[] {
if (points == null) {
return []
}
const aspects = []
for (const point in points) {
if (points.hasOwnProperty(point)) {
for (const toPoint in this.toPoints) {
if (this.toPoints.hasOwnProperty(toPoint)) {
for (const aspect in this.settings.ASPECTS) {
if (this.hasAspect(points[point][0], this.toPoints[toPoint][0], this.settings.ASPECTS[aspect])) {
let precision = this.calcPrecision(points[point][0], this.toPoints[toPoint][0], this.settings.ASPECTS[aspect].degree)
// -1 : is approaching to aspect
// +1 : is moving away
if (this.isTransitPointApproachingToAspect(this.settings.ASPECTS[aspect].degree, this.toPoints[toPoint][0], points[point][0])) {
precision *= -1
}
// if transit has speed value && transit is retrograde
if (points[point][1] && points[point][1] < 0) {
precision *= -1
}
aspects.push(
{
aspect: { name: aspect, degree: this.settings.ASPECTS[aspect].degree, orbit: this.settings.ASPECTS[aspect].orbit, color: this.settings.ASPECTS[aspect].color, width: this.settings.ASPECTS[aspect].width },
point: { name: point, position: points[point][0] },
toPoint: { name: toPoint, position: this.toPoints[toPoint][0] },
precision: precision.toFixed(4)
}
)
}
}
}
}
}
}
return aspects.sort(this.compareAspectsByPrecision)
}
/*
* @private
* @param {double} point
* @param {double} toPoint
* @param {Array} aspects; [DEGREE, ORBIT]
*/
hasAspect (point: number, toPoint: number, aspect: AspectData): boolean {
let result = false
let gap = Math.abs(point - toPoint)
if (gap > radiansToDegree(Math.PI)) {
gap = radiansToDegree(2 * Math.PI) - gap
}
const orbitMin = aspect.degree - (aspect.orbit / 2)
const orbitMax = aspect.degree + (aspect.orbit / 2)
if (orbitMin <= gap && gap <= orbitMax) {
result = true
}
return result
}
/*
* @private
* @param {Object} pointAngle
* @param {Object} toPointAngle
* @param {double} aspectDegree;
*/
calcPrecision (point: number, toPoint: number, aspect: number): number {
let gap = Math.abs(point - toPoint)
if (gap > radiansToDegree(Math.PI)) {
gap = radiansToDegree(2 * Math.PI) - gap
}
return Math.abs(gap - aspect)
}
/*
* Calculate direction of aspect
* whether the transiting planet is approaching or is falling
* @private
*
* //TODO
* This method is tested, and for tests gives the right results.
* But the code is totally unclear. It needs to be rewritten.
* @param {double} aspect - aspect degree; for example 90.
* @param {double} toPoint - angle of standing point
* @param {double} point - angle of transiting planet
* @return {boolean}
*/
isTransitPointApproachingToAspect (aspect: number, toPoint: number, point: number): boolean {
if ((point - toPoint) > 0) {
if ((point - toPoint) > radiansToDegree(Math.PI)) {
point = (point + aspect) % radiansToDegree(2 * Math.PI)
} else {
toPoint = (toPoint + aspect) % radiansToDegree(2 * Math.PI)
}
} else {
if ((toPoint - point) > radiansToDegree(Math.PI)) {
toPoint = (toPoint + aspect) % radiansToDegree(2 * Math.PI)
} else {
point = (point + aspect) % radiansToDegree(2 * Math.PI)
}
}
let _point = point
let _toPoint = toPoint
const difference = _point - _toPoint
if (Math.abs(difference) > radiansToDegree(Math.PI)) {
_point = toPoint
_toPoint = point
}
return (_point - _toPoint < 0)
}
/*
* Aspects comparator
* by precision
* @private
* @param {Object} a
* @param {Object} b
*/
compareAspectsByPrecision (a: FormedAspect, b: FormedAspect): number {
return parseFloat(a.precision) - parseFloat(b.precision)
}
}
export default AspectCalculator