Skip to content

Instantly share code, notes, and snippets.

@Efimero
Created February 10, 2018 19:38
Show Gist options
  • Select an option

  • Save Efimero/0287dff1df965c56cbd046399c2bfeda to your computer and use it in GitHub Desktop.

Select an option

Save Efimero/0287dff1df965c56cbd046399c2bfeda to your computer and use it in GitHub Desktop.
(def tau (* 2 Math/PI))
(defn clamp [n minimum maximum]
(min (max n minimum) maximum))
(defn sph-v [long lat alt]; A polar vector
{:longitude (mod long 360)
:latitude (clamp lat -90 90)
:altitude (max alt 0)})
(defn uniform [n]; A list of equidistant fractions from 0 to 1
(map #(/ % n) (range n)))
(defn long-transform [n]
(* tau n))
(defn lat-transform [n]
(Math/acos (- (* 2 n) 1)))
(defn sphere-hmap [resolution]; A list of uniformly distributed vectors on the surface of a sphere
(for [u (map uniform (range n))
v (map uniform (range n))]
(sph-v (long-transform u) (lat-transform v) 1)))
(defn pythagorean [x y]
(Math/sqrt (+ (Math/sqr x) (Math/sqr y))))
(defn great-circles [theta phi]; Distance across the sphere surface
(* 2
(Math/arcsin2 (Math/sqrt
(Math/sqr (Math/sin (/ phi 2)))
(* (Math/cos phi)
(Math/sqr (Math/sin (/ theta 2))))))))
(defn surface-distance [along alat ar blong blat br]; Distance across the sphere surface with height difference
(pythagorean
(great-circles
(Math/abs (mod (- along blong) 180))
(Math/abs (- alat blat)))
(Math/abs (- ar br))))
(defn square-smooth [sphere long lat r]; Smoothing function over a sphere
(map
(fn [p]
(update p :altitude ;for each point, change the height
(fn [n]
(* (/ (+ n r) 2) ;mean of the point we're smoothing and the reference point
(let [x (surface-distance ; the further the point we're smoothing is from the reference point, the less we alter it
long lat r
(:longitude p) (:latitude p) (:altitude p))]
(Math/pow 2 (Math/pow (* -1 x) 2)))))))
sphere))
(defn smooth-sphere [sphere];processes a whole sphere and
(reduce
(fn [sph p];for each point in the sphere
(map #(update % :altitude
(/ (+ (:altitude p) (:altitude %)) 2));get the mean of the resultant sphere
(square-smooth sphere (:longitude p) (:latitude p) (:altitude p))));and a sphere smoothed for that point
sphere))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment