-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconv.go
73 lines (60 loc) · 2.13 KB
/
conv.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
package glm
import (
"github.com/EngoEngine/math"
)
// CartesianToSpherical converts 3-dimensional cartesian coordinates (x,y,z) to
// spherical coordinates with radius r, inclination theta, and azimuth phi.
func CartesianToSpherical(coord Vec3) (r, theta, phi float32) {
r = coord.Len()
theta = math.Acos(coord[2] / r)
phi = math.Atan2(coord[1], coord[0])
return
}
// SphericalToCartesian converts spherical coordinates with radius r,
// inclination theta, and azimuth phi to cartesian coordinates (x,y,z).
func SphericalToCartesian(r, theta, phi float32) Vec3 {
st, ct := math.Sincos(theta)
sp, cp := math.Sincos(phi)
return Vec3{r * st * cp, r * st * sp, r * ct}
}
// CartesianToCylindrical converts 3-dimensional cartesian coordinates (x,y,z)
// to cylindrical coordinates with radial distance r, azimuth phi, and height z.
func CartesianToCylindrical(coord Vec3) (rho, phi, z float32) {
rho = math.Hypot(coord[0], coord[1])
phi = math.Atan2(coord[1], coord[0])
z = coord[2]
return
}
// CylindricalToCartesian converts cylindrical coordinates with radial distance
// r, azimuth phi, and height z to cartesian coordinates (x,y,z).
func CylindricalToCartesian(rho, phi, z float32) Vec3 {
s, c := math.Sincos(phi)
return Vec3{rho * c, rho * s, z}
}
// SphericalToCylindrical converts spherical coordinates with radius r,
// inclination theta, and azimuth phi to cylindrical coordinates with radial
// distance r, azimuth phi, and height z.
func SphericalToCylindrical(r, theta, phi float32) (rho, phi2, z float32) {
s, c := math.Sincos(theta)
rho = r * s
z = r * c
phi2 = phi
return
}
// CylindricalToSpherical converts cylindrical coordinates with radial distance
// r, azimuth phi, and height z to spherical coordinates with radius r,
// inclination theta, and azimuth phi.
func CylindricalToSpherical(rho, phi, z float32) (r, theta, phi2 float32) {
r = math.Hypot(rho, z)
phi2 = phi
theta = math.Atan2(rho, z)
return
}
// DegToRad converts degrees to radians
func DegToRad(angle float32) float32 {
return angle * math.Pi / 180
}
// RadToDeg converts radians to degrees
func RadToDeg(angle float32) float32 {
return angle * 180 / math.Pi
}