SHARE
TWEET

Untitled

a guest Apr 21st, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns collijion.planet
  2.   (:import [clojure.lang PersistentVector]))
  3.  
  4. (def G 6.674E-11)
  5.  
  6. (defrecord Planet [m r x y ^PersistentVector v])
  7.  
  8. (defn add
  9.   "Adds a vector to another"
  10.   [a b]
  11.   (mapv + a b))
  12.  
  13. (defn sub
  14.   "Subtracts a vector from another"
  15.   [a b]
  16.   (mapv - a b))
  17.  
  18. (defn mult
  19.   "Multiplies a vector by a number"
  20.   [v n]
  21.   (mapv #(* % n) v))
  22.  
  23. (defn div
  24.   "Divides a vector by a number"
  25.   [v n]
  26.   (when-not (= n 0)
  27.    (mapv #(/ % n) v)))
  28.  
  29. (defn distance
  30.   "Calculates the Euclidean distance between
  31.  two points"
  32.   [p1 p2]
  33.   (Math/sqrt
  34.    (+ (Math/pow (- (:x p1) (:x p2)) 2)
  35.       (Math/pow (- (:y p1) (:y p2)) 2))))
  36.  
  37. (defn magnitude
  38.   "Calculates the magnitude of a vector"
  39.   [[x y]]
  40.   (Math/sqrt (+ (Math/pow x 2) (Math/pow y 2))))
  41.  
  42. (defn unit
  43.   "Calculates the unit vector of a vector"
  44.   [v]
  45.   (let [m (magnitude v)]
  46.     (if (= m 0)
  47.       [0 0]
  48.       (div v m))))
  49.  
  50. (defn grav-vec
  51.   "Calculates the attraction force between two
  52.  bodies using Newton's law of universal gravitation.
  53.  Force applied on object 2 exerted by object 1."
  54.   [p1 p2]
  55.   (let [dist (distance p2 p1)]
  56.     (mult
  57.      (div (sub (:v p2) (:v p1)) dist)
  58.      (* (- G)
  59.        (/ (* (:m p1) (:m p2))
  60.           (Math/pow dist 2))))))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top