Skip to content

Commit 5d7859b

Browse files
committed
enhance math ns
1 parent 959e2de commit 5d7859b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/renderer/tool/misc/measure.cljs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
(ns renderer.tool.misc.measure
22
(:require
33
[clojure.core.matrix :as mat]
4-
[goog.math]
54
[re-frame.core :as rf]
65
[renderer.document.subs :as-alias document.s]
76
[renderer.element.handlers :as element.h]
87
[renderer.handlers :as handlers]
98
[renderer.tool.base :as tool]
109
[renderer.tool.overlay :as overlay]
10+
[renderer.utils.math :as math]
1111
[renderer.utils.units :as units]))
1212

1313
(derive :measure ::tool/tool)
@@ -53,7 +53,7 @@
5353
[{:keys [attrs key hypotenuse]}]
5454
(let [{:keys [x1 x2 y1 y2]} attrs
5555
[x1 y1 x2 y2] (map units/unit->px [x1 y1 x2 y2])
56-
angle (goog.math/angle x1 y1 x2 y2)
56+
angle (math/angle x1 y1 x2 y2)
5757
zoom @(rf/subscribe [::document.s/zoom])
5858
straight? (< angle 180)
5959
straight-angle (if straight? angle (- angle 360))]

src/renderer/utils/math.cljs

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[clojure.math :as math]))
44

55
(defn clamp
6+
"Clamps a number within the provided bounds."
67
[x minimum maximum]
78
(min (max x minimum) maximum))
89

@@ -13,3 +14,17 @@
1314
(defn angle-dy
1415
[degrees radius]
1516
(* radius (Math/sin (math/to-radians degrees))))
17+
18+
(defn normalize-angle
19+
"Normalizes an angle to be in range [0-360). Angles outside this range will
20+
be normalized to be the equivalent angle with that range."
21+
[angle]
22+
(mod angle (* 2 Math/PI)))
23+
24+
(defn angle
25+
[x1 y1 x2 y2]
26+
(-> (Math/atan2 (- y2 y1) (- x2 x1))
27+
normalize-angle
28+
math/to-degrees))
29+
30+

0 commit comments

Comments
 (0)