0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-10 17:18:21 -05:00

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

View file

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

View file

@ -139,12 +139,18 @@
#?(:cljs (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
"Calculate the distance between two points."
[[x1 y1] [x2 y2]]
(let [dx (- x1 x2)
dy (- y1 y2)]
(-> (sqrt (+ (pow dx 2) (pow dy 2)))
(-> (hypot dx dy)
(precision 2))))
(defn log10
@ -182,3 +188,4 @@
"Get the sign (+1 / -1) for the number"
[n]
(if (neg? n) -1 1))

View file

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