-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangle.go
43 lines (35 loc) · 1.01 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
package math
import (
"fmt"
"math"
)
const radiansCircle = float32(2) * float32(math.Pi)
const sexagesimal = float32(360)
// An angle expressed in radians
type Angle float32
// If the angle is bigger than a complete rotation, simplify it removing the extra rotations
func (angle Angle) Simplify() Angle {
if angle < 0 {
return Angle(float32(angle) + radiansCircle).Simplify()
} else if float32(angle) < radiansCircle {
return angle
} else {
return Angle(float32(angle) - radiansCircle).Simplify()
}
}
func (angle Angle) Equivalent(a Angle) bool {
return angle.Simplify() == a.Simplify()
}
func (angle Angle) ToString() string {
if float32(angle) >= 0 || float32(angle) < radiansCircle {
return fmt.Sprintf("%.2fº (%.5f rad)", angle.ToSexagesimal(), angle)
} else {
return fmt.Sprintf("%f rad", angle)
}
}
func AngleFromSexagesimal(angle float32) Angle {
return Angle(angle / sexagesimal * radiansCircle)
}
func (angle Angle) ToSexagesimal() float32 {
return float32(angle) * sexagesimal / radiansCircle
}