Skip to content

Instantly share code, notes, and snippets.

@xorgy
Last active December 21, 2015 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xorgy/6345925 to your computer and use it in GitHub Desktop.
Save xorgy/6345925 to your computer and use it in GitHub Desktop.
Util for converting between Cartesian coordinates and spherical coordinates(with the physics symbolic convention).
(define (xyz->ρ x y z)
(sqrt (+
(expt x 2)
(expt y 2)
(expt z 2))))
(define (zρ->θ z ρ)
(acos (/ z ρ)))
(define (yx->φ y x)
(atan (/ y x)))
(define (xyz->ρθφ x y z)
(define (ρ) (xyz->ρ x y z))
(define (θ) (zρ->θ z (ρ)))
(define (φ) (yx->φ y x))
(list (ρ) (θ) (φ)))
(define (ρθφ->x ρ θ φ)
(*
ρ
(sin θ)
(cos φ)))
(define (ρθφ->y ρ θ φ)
(*
ρ
(sin θ)
(sin φ)))
(define (ρθ->z ρ θ)
(* ρ (cos θ)))
(define (ρθφ->xyz ρ θ φ)
(define (x) (ρθφ->x ρ θ φ))
(define (y) (ρθφ->y ρ θ φ))
(define (z) (ρθ->z ρ θ))
(list (x) (y) (z)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment