-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
93 lines (74 loc) · 1.83 KB
/
types.go
File metadata and controls
93 lines (74 loc) · 1.83 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
package main
import (
"fmt"
geo "github.com/paulmach/go.geo"
"math"
"time"
"strconv"
)
type Waypoint struct {
*geo.Point
Time time.Time `json:"time,string"`
}
// Velocity is measured in m/s
type Velocity float64
func NewVelocityFromMph(mph float64) Velocity {
return Velocity(mph * 1609.34 / 3600.)
}
// Mph computes the velocity in miles per hour.
func (v Velocity) Mph() float64 {
return float64(v/1609.34) * 3600
}
// Ms returns the velocity in meters per second.
func (v Velocity) Ms() float64 {
return float64(v)
}
// Bearing is measured in radians.
type Bearing float64
// NewBearing creates a Bearing at r radians.
func NewBearing(r float64) Bearing {
return Bearing(r).Normalize()
}
// NewBearingFromDegrees creates a Bearing at r radians.
func NewBearingFromDegrees(d float64) Bearing {
return Bearing(deg2Rad(d)).Normalize()
}
func (b Bearing) Normalize() Bearing {
if b < 0 {
b += 2 * math.Pi
}
return b
}
// Degrees returns the Bearing in degrees from due North.
func (b Bearing) Degrees() float64 {
return float64(360.0 * b / (2 * math.Pi))
}
// OClock returns the Bearing as represented on a 12 hour clock face.
// e.g. 270 degrees is 9 o'clock
func (b Bearing) OClock() float64 {
return b.Degrees() * 12. / 360.
}
// Radians returns the Bearing in radians between 0 and 2π
func (b Bearing) Radians() float64 {
return float64(b)
}
func (b Bearing) String() string {
return fmt.Sprintf("%3.f°", b.Degrees())
}
func (b Bearing) UnmarshalJSON(a []byte) error {
deg, err := strconv.ParseFloat(string(a), 10);
if err != nil {
return err
}
b = NewBearingFromDegrees(deg)
return nil
}
func (b Bearing) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%3.f", b.Degrees())), nil
}
func deg2Rad(d float64) float64 {
return d * math.Pi / 180.0
}
func rad2Deg(r float64) float64 {
return 180.0 * r / math.Pi
}