-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathconv.go
96 lines (76 loc) · 2.76 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// This file is generated from mgl32/conv.go; DO NOT EDIT
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl64
import (
"math"
)
// CartesianToSpherical converts 3-dimensional cartesian coordinates (x,y,z) to spherical
// coordinates with radius r, inclination theta, and azimuth phi.
//
// All angles are in radians.
func CartesianToSpherical(coord Vec3) (r, theta, phi float64) {
r = coord.Len()
theta = float64(math.Acos(float64(coord[2] / r)))
phi = float64(math.Atan2(float64(coord[1]), float64(coord[0])))
return
}
// CartesianToCylindical converts 3-dimensional cartesian coordinates (x,y,z) to
// cylindrical coordinates with radial distance r, azimuth phi, and height z.
//
// All angles are in radians.
func CartesianToCylindical(coord Vec3) (rho, phi, z float64) {
rho = float64(math.Hypot(float64(coord[0]), float64(coord[1])))
phi = float64(math.Atan2(float64(coord[1]), float64(coord[0])))
z = coord[2]
return
}
// SphericalToCartesian converts spherical coordinates with radius r, inclination theta,
// and azimuth phi to cartesian coordinates (x,y,z).
//
// Angles are in radians.
func SphericalToCartesian(r, theta, phi float64) Vec3 {
st, ct := math.Sincos(float64(theta))
sp, cp := math.Sincos(float64(phi))
return Vec3{r * float64(st*cp), r * float64(st*sp), r * float64(ct)}
}
// SphericalToCylindrical converts spherical coordinates with radius r,
// inclination theta, and azimuth phi to cylindrical coordinates with radial
// distance r, azimuth phi, and height z.
//
// Angles are in radians
func SphericalToCylindrical(r, theta, phi float64) (rho, phi2, z float64) {
s, c := math.Sincos(float64(theta))
rho = r * float64(s)
z = r * float64(c)
phi2 = phi
return
}
// CylindircalToSpherical converts cylindrical coordinates with radial distance
// r, azimuth phi, and height z to spherical coordinates with radius r,
// inclination theta, and azimuth phi.
//
// Angles are in radians
func CylindircalToSpherical(rho, phi, z float64) (r, theta, phi2 float64) {
r = float64(math.Hypot(float64(rho), float64(z)))
phi2 = phi
theta = float64(math.Atan2(float64(rho), float64(z)))
return
}
// CylindricalToCartesian converts cylindrical coordinates with radial distance
// r, azimuth phi, and height z to cartesian coordinates (x,y,z)
//
// Angles are in radians.
func CylindricalToCartesian(rho, phi, z float64) Vec3 {
s, c := math.Sincos(float64(phi))
return Vec3{rho * float64(c), rho * float64(s), z}
}
// DegToRad converts degrees to radians
func DegToRad(angle float64) float64 {
return angle * float64(math.Pi) / 180
}
// RadToDeg converts radians to degrees
func RadToDeg(angle float64) float64 {
return angle * 180 / float64(math.Pi)
}