From b102c19ea758eb42b0c37df69d8757b4a4ef5a03 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 6 Feb 2016 12:25:39 +0200 Subject: [PATCH] Minor fixes and improvements to point geom impl. --- src/uxbox/util/geom/point.cljs | 39 +++++++++++++++++++++++---------- test/uxbox/util/geom_tests.cljs | 2 +- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/uxbox/util/geom/point.cljs b/src/uxbox/util/geom/point.cljs index c2674d915a..7fd366fb68 100644 --- a/src/uxbox/util/geom/point.cljs +++ b/src/uxbox/util/geom/point.cljs @@ -60,7 +60,7 @@ (Point. (+ (:x p) (:x other)) (+ (:y p) (:y other))))) -(defn substract +(defn subtract "Returns the subtraction of the supplied value to both coordinates of the point as a new point." [p other] @@ -94,17 +94,32 @@ {:pre [(point? p)]} (-> (mth/atan2 (:y p) (:x p)) (mth/degrees))) - ([p other] - {:pre [(point? p)]} - (let [other (-point other) - a (/ (+ (* (:x p) (:x other)) - (* (:y p) (:y other))) - (* (length p) (length other))) - a (mth/acos (if (< a -1) - -1 - (if (> a 1) 1 a)))] - (-> (mth/degrees a) - (mth/precision 6))))) + ([p center] + (let [center (-point center)] + (angle (subtract p center))))) + +(defn angle-with-other + "Consider point as vector and calculate + the angle between two vectors." + [p other] + {:pre [(point? p)]} + (let [other (-point other) + a (/ (+ (* (:x p) (:x other)) + (* (:y p) (:y other))) + (* (length p) (length other))) + a (mth/acos (if (< a -1) + -1 + (if (> a 1) 1 a)))] + (-> (mth/degrees a) + (mth/precision 6)))) + +(defn update-angle + "Update the angle of the point." + [p angle] + (let [len (length p) + angle (mth/radians angle)] + (Point. (* (mth/cos angle) len) + (* (mth/sin angle) len)))) (defn quadrant "Return the quadrant of the angle of the point." diff --git a/test/uxbox/util/geom_tests.cljs b/test/uxbox/util/geom_tests.cljs index f1d91c33f1..8511a6c1cf 100644 --- a/test/uxbox/util/geom_tests.cljs +++ b/test/uxbox/util/geom_tests.cljs @@ -65,7 +65,7 @@ (t/is (= angle 90))) (let [p1 (gpt/point 0 10) p2 (gpt/point 10 10) - angle (gpt/angle p1 p2)] + angle (gpt/angle-with-other p1 p2)] (t/is (number? angle)) (t/is (= angle 45))))