Skip to content

Instantly share code, notes, and snippets.

@topokel
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save topokel/a67f6052e782e15f5c6c to your computer and use it in GitHub Desktop.

Select an option

Save topokel/a67f6052e782e15f5c6c to your computer and use it in GitHub Desktop.
package main
import "math"
type CartesianPoint struct {
x, y, z float32
}
// set Spherical location from Cartesian location
func (vector *CartesianPoint) ToSpherical () SphericalPoint {
// only calculate once when used twice
radius := math.Sqrt(math.Pow(vector.x, 2) + math.Pow(vector.y, 2) + math.Pow(vector.z, 2))
return SphericalPoint{
radius: radius,
theta: math.Acos(vector.z / radius),
phi: math.Atan(vector.y / vector.x)
}
}
// spherical coordinates of Instance
// asimuth/phi is North/South, polar/theta is East/West
// azimuth/phi and polar/theta are in radians
type SphericalPoint struct {
radius, theta, phi float32
}
// set Cartesian location from Spherical location
func (vector *SphericalPoint) ToCartesian () CartesianPoint {
// only calculate the sine of azimuth once
sinPhi := math.Sin(vector.phi)
return CartesianVector{
x: vector.radius * sinPhi * math.Cos(vector.theta),
y: vector.radius * sinPhi * math.Sin(vector.theta),
z: vector.radius * math.Cos(vector.phi)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment