From fc4b3ebee92dea75d2bf4e99aeeefde709ff645b Mon Sep 17 00:00:00 2001 From: Charles Comstock Date: Wed, 9 Mar 2022 17:09:36 -0600 Subject: [PATCH] Add argument parsing for triangle/circumcircle to improve coverage Handles receiving a triangle, a map with points a,b,c and a sequential tuple of three elements. Also adds tests to cover these cases. --- src/thi/ng/geom/triangle.cljc | 12 +++++++++++- test/thi/ng/geom/test/types/triangle.cljc | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/thi/ng/geom/test/types/triangle.cljc diff --git a/src/thi/ng/geom/triangle.cljc b/src/thi/ng/geom/triangle.cljc index 5a0aff1e..52de266a 100644 --- a/src/thi/ng/geom/triangle.cljc +++ b/src/thi/ng/geom/triangle.cljc @@ -159,7 +159,17 @@ [o (g/dist o b)])))) (defn circumcircle - ([t] (circumcircle (get t :a) (get t :b) (get t :c))) + ([t] + (cond (instance? Triangle2 t) + (let [{[a b c] :points} t] + (circumcircle a b c)) + (map? t) + (let [{:keys [a b c]} t] + (circumcircle a b c)) + (sequential? t) + (let [[a b c] t] + (circumcircle a b c)) + :else (err/illegal-arg! t))) ([a b c] (let [[o r] (circumcircle-raw a b c)] (Circle2. o r)))) diff --git a/test/thi/ng/geom/test/types/triangle.cljc b/test/thi/ng/geom/test/types/triangle.cljc new file mode 100644 index 00000000..ebcda7e4 --- /dev/null +++ b/test/thi/ng/geom/test/types/triangle.cljc @@ -0,0 +1,23 @@ +(ns thi.ng.geom.test.types.triangle + #?(:cljs + (:require-macros + [cemerick.cljs.test :refer (is deftest)])) + (:require + [thi.ng.geom.types] + [thi.ng.geom.circle :as c] + [thi.ng.geom.triangle :as t] + [thi.ng.geom.vector :as v] + #?(:clj + [clojure.test :refer [is deftest]] + :cljs + [cemerick.cljs.test]))) + +(deftest test-circumcircle + (let [a (v/vec2 0 0) + b (v/vec2 4 0) + c (v/vec2 0 2) + expected (c/circle (v/vec2 2.0 0.0) 2.0)] + (is (= expected (t/circumcircle a b c))) + (is (= expected (t/circumcircle (t/triangle2 a b c)))) + (is (= expected (t/circumcircle {:a a :b b :c c}))) + (is (= expected (t/circumcircle [a b c])))))