-
Notifications
You must be signed in to change notification settings - Fork 0
/
angle.go
126 lines (98 loc) · 1.94 KB
/
angle.go
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
package goastro
import (
"fmt"
"math"
)
type Angle float64 // degrees
func (a Angle) Degrees() float64 {
return float64(a)
}
func (a Angle) Radians() float64 {
return float64(a) / 180 * math.Pi
}
func (a Angle) Hours() float64 {
return a.Degrees() / 15
}
func (a Angle) ArcMinutes() float64 {
return a.Degrees() * 60
}
func (a Angle) ArcSeconds() float64 {
return a.Degrees() * 3600
}
func (a Angle) String() string {
if a.IsNaN() {
return "NaN°"
}
d, df := math.Modf(a.Degrees())
m, mf := math.Modf(df * 60)
s, sf := math.Modf(mf * 60)
return fmt.Sprintf("%d°%d'%d\".%d/%.5f", int(d), int(m), int(s), int(sf*1000), a.Degrees())
}
func (a Angle) Sin() float64 {
return sin(a)
}
func (a Angle) Cos() float64 {
return cos(a)
}
func (a Angle) Tan() float64 {
return tan(a)
}
func (a Angle) IsNaN() bool {
return math.IsNaN(float64(a))
}
func Degrees(d float64) Angle {
return Angle(d)
}
func ArcMinutes(s float64) Angle {
return Angle(s / 60)
}
func ArcSeconds(s float64) Angle {
return Angle(s / 3600)
}
func Radians(r float64) Angle {
return Angle(r / math.Pi * 180)
}
func Hours(h float64) Angle {
return Angle(h * 15)
}
func (a Angle) Normalize() Angle {
d := math.Mod(float64(a), 360)
if d < 0 {
d += 360
}
return Angle(d)
}
func (a Angle) Normalize180() Angle {
a = a.Normalize()
if a.Degrees() >= 180 {
return a - Degrees(360)
}
return a
}
func sin(a Angle) float64 {
return math.Sin(a.Radians())
}
func cos(a Angle) float64 {
return math.Cos(a.Radians())
}
func tan(a Angle) float64 {
return math.Tan(a.Radians())
}
func asin(x float64) Angle {
return Radians(math.Asin(x))
}
func acos(x float64) Angle {
return Radians(math.Acos(x))
}
func atan(x float64) Angle {
return Radians(math.Atan(x))
}
func Atan(x float64) Angle {
return Radians(math.Atan(x))
}
func atan2(y float64, x float64) Angle {
return Radians(math.Atan2(y, x))
}
func Atan2(y float64, x float64) Angle {
return Radians(math.Atan2(y, x))
}