Use the function hypot for distances

This commit is contained in:
alonso.torres 2023-01-04 16:18:12 +01:00
parent 84e9f69213
commit 10439934d4
4 changed files with 12 additions and 8 deletions

View file

@ -170,8 +170,7 @@
(dm/get-prop p2 :x)) (dm/get-prop p2 :x))
dy (- (dm/get-prop p1 :y) dy (- (dm/get-prop p1 :y)
(dm/get-prop p2 :y))] (dm/get-prop p2 :y))]
(mth/sqrt (+ (mth/pow dx 2) (mth/hypot dx dy)))
(mth/pow dy 2)))))
(defn distance-vector (defn distance-vector
"Calculate the distance, separated x and y." "Calculate the distance, separated x and y."
@ -191,8 +190,7 @@
(assert (point? pt) "point instance expected") (assert (point? pt) "point instance expected")
(let [x (dm/get-prop pt :x) (let [x (dm/get-prop pt :x)
y (dm/get-prop pt :y)] y (dm/get-prop pt :y)]
(mth/sqrt (+ (mth/pow x 2) (mth/hypot x y)))
(mth/pow y 2)))))
(defn angle (defn angle
"Returns the smaller angle between two vectors. "Returns the smaller angle between two vectors.

View file

@ -117,7 +117,7 @@
[x y] (->> coords (mapv solve-derivative)) [x y] (->> coords (mapv solve-derivative))
;; normalize value ;; normalize value
d (mth/sqrt (+ (* x x) (* y y)))] d (mth/hypot x y)]
(if (mth/almost-zero? d) (if (mth/almost-zero? d)
(gpt/point 0 0) (gpt/point 0 0)

View file

@ -139,12 +139,18 @@
#?(:cljs (math/toDegrees radians) #?(:cljs (math/toDegrees radians)
:clj (Math/toDegrees radians))) :clj (Math/toDegrees radians)))
(defn hypot
"Square root of the squares addition"
[a b]
#?(:cljs (js/Math.hypot a b)
:clj (Math/hypot a b)))
(defn distance (defn distance
"Calculate the distance between two points." "Calculate the distance between two points."
[[x1 y1] [x2 y2]] [[x1 y1] [x2 y2]]
(let [dx (- x1 x2) (let [dx (- x1 x2)
dy (- y1 y2)] dy (- y1 y2)]
(-> (sqrt (+ (pow dx 2) (pow dy 2))) (-> (hypot dx dy)
(precision 2)))) (precision 2))))
(defn log10 (defn log10
@ -182,3 +188,4 @@
"Get the sign (+1 / -1) for the number" "Get the sign (+1 / -1) for the number"
[n] [n]
(if (neg? n) -1 1)) (if (neg? n) -1 1))

View file

@ -890,8 +890,7 @@
(defn calculate-ratio (defn calculate-ratio
;; sqrt((actual-width)**2 + (actual-height)**2)/sqrt(2). ;; sqrt((actual-width)**2 + (actual-height)**2)/sqrt(2).
[width height] [width height]
(/ (mth/sqrt (+ (mth/pow width 2) (/ (mth/hypot width height)
(mth/pow height 2)))
(mth/sqrt 2))) (mth/sqrt 2)))
(defn fix-percents (defn fix-percents