Minor fixes and improvements to point geom impl.

This commit is contained in:
Andrey Antukh 2016-02-06 12:25:39 +02:00
parent 6a1dad581e
commit b102c19ea7
2 changed files with 28 additions and 13 deletions

View File

@ -60,7 +60,7 @@
(Point. (+ (:x p) (:x other)) (Point. (+ (:x p) (:x other))
(+ (:y p) (:y other))))) (+ (:y p) (:y other)))))
(defn substract (defn subtract
"Returns the subtraction of the supplied value to both "Returns the subtraction of the supplied value to both
coordinates of the point as a new point." coordinates of the point as a new point."
[p other] [p other]
@ -94,7 +94,14 @@
{:pre [(point? p)]} {:pre [(point? p)]}
(-> (mth/atan2 (:y p) (:x p)) (-> (mth/atan2 (:y p) (:x p))
(mth/degrees))) (mth/degrees)))
([p other] ([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)]} {:pre [(point? p)]}
(let [other (-point other) (let [other (-point other)
a (/ (+ (* (:x p) (:x other)) a (/ (+ (* (:x p) (:x other))
@ -104,7 +111,15 @@
-1 -1
(if (> a 1) 1 a)))] (if (> a 1) 1 a)))]
(-> (mth/degrees a) (-> (mth/degrees a)
(mth/precision 6))))) (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 (defn quadrant
"Return the quadrant of the angle of the point." "Return the quadrant of the angle of the point."

View File

@ -65,7 +65,7 @@
(t/is (= angle 90))) (t/is (= angle 90)))
(let [p1 (gpt/point 0 10) (let [p1 (gpt/point 0 10)
p2 (gpt/point 10 10) p2 (gpt/point 10 10)
angle (gpt/angle p1 p2)] angle (gpt/angle-with-other p1 p2)]
(t/is (number? angle)) (t/is (number? angle))
(t/is (= angle 45)))) (t/is (= angle 45))))