-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangle.go
191 lines (157 loc) · 2.98 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
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
package dms
import (
"fmt"
"math"
"strings"
)
type Unit int
const (
DegUnit Unit = iota
MinUnit
SecUnit
)
const pi180 = math.Pi / 180.0
type axis int
const (
NoAxis axis = iota
LatAxis
LonAxis
)
func Sign(v string) int {
switch v {
case SouthType, WestType, "-":
return -1
case NorthType, EastType, "+":
return 1
case "":
return 1
}
return 0
}
func HemiLat(sign int) string {
if sign >= 0 {
return NorthType
}
return SouthType
}
func HemiLon(sign int) string {
if sign >= 0 {
return EastType
}
return WestType
}
func hemi(a axis, sign int) string {
switch a {
case LatAxis:
return HemiLat(sign)
case LonAxis:
return HemiLon(sign)
}
return ""
}
type Fields struct {
Deg string
Min string
Sec string
DegSym string
MinSym string
SecSym string
Hemi string
}
func (f Fields) IsDD() bool {
return f.Min == "" && f.Sec == ""
}
func (f Fields) IsDM() bool {
return f.Min != "" && f.Sec == ""
}
func (f Fields) IsDMS() bool {
return f.Min != "" && f.Sec != ""
}
func (f Fields) String() string {
var buf strings.Builder
sign := ""
if f.Hemi == "-" || f.Hemi == "+" {
sign = f.Hemi
}
degSym := f.DegSym
if degSym == "" {
degSym = "°"
}
fmt.Fprintf(&buf, "%v%v%v", sign, f.Deg, degSym)
if f.Min != "" {
minSym := f.MinSym
if minSym == "" {
minSym = "′"
}
fmt.Fprintf(&buf, " %v%v", f.Min, minSym)
}
if f.Sec != "" {
secSym := f.SecSym
if secSym == "" {
secSym = "″"
}
fmt.Fprintf(&buf, " %v%v", f.Sec, secSym)
}
if sign == "" && f.Hemi != "" {
fmt.Fprintf(&buf, " %v", f.Hemi)
}
return buf.String()
}
type Angle struct {
deg float64
min float64
sec float64
}
func NewAngle(deg float64, min float64, sec float64) Angle {
sign := 1.
if deg < 0 {
sign = -1.
}
deg, min, sec = math.Abs(deg), math.Abs(min), math.Abs(sec)
// Normalize floats
if ideg, fdeg := math.Modf(deg); fdeg != 0 {
deg = ideg
min += math.Abs(fdeg) * 60
}
if imin, fmin := math.Modf(min); fmin != 0 {
min = imin
sec += math.Abs(fmin) * 60
}
return Angle{}.Add(Angle{
deg: deg * sign,
min: min * sign,
sec: sec * sign,
})
}
func (a Angle) String() string {
return fmt.Sprintf("(%v,%v,%v)", a.deg, a.min, a.sec)
}
func (a Angle) Add(a2 Angle) Angle {
var carry float64
sec := a.sec + a2.sec
carry, a.sec = float64(int(sec/60)), math.Mod(sec, 60)
min := a.min + a2.min + carry
carry, a.min = float64(int(min/60)), math.Mod(min, 60)
a.deg = a.deg + a2.deg + carry
return a
}
func (a Angle) Sub(a2 Angle) Angle {
a2neg := Angle{deg: -a2.deg, min: -a2.min, sec: -a2.sec}
return a.Add(a2neg)
}
func (a Angle) Degrees() float64 {
return a.deg + (a.min / 60) + (a.sec / 3600)
}
func (a Angle) Minutes() float64 {
return (a.deg * 60) + a.min + (a.sec / 60)
}
func (a Angle) Seconds() float64 {
return (a.deg * 3600) + (a.min * 60) + a.sec
}
func (a Angle) Radians() float64 {
return a.Degrees() * pi180
}
func (a Angle) DMS() (deg, min, sec float64) {
deg, min, sec = a.deg, math.Abs(a.min), math.Abs(a.sec)
return
}