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,17 +94,32 @@
{: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]
{:pre [(point? p)]} (let [center (-point center)]
(let [other (-point other) (angle (subtract p center)))))
a (/ (+ (* (:x p) (:x other))
(* (:y p) (:y other))) (defn angle-with-other
(* (length p) (length other))) "Consider point as vector and calculate
a (mth/acos (if (< a -1) the angle between two vectors."
-1 [p other]
(if (> a 1) 1 a)))] {:pre [(point? p)]}
(-> (mth/degrees a) (let [other (-point other)
(mth/precision 6))))) 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 (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))))