diff --git a/src/shimmers/math/geometry/collisions.cljc b/src/shimmers/math/geometry/collisions.cljc index 043679a1..bec4af3a 100644 --- a/src/shimmers/math/geometry/collisions.cljc +++ b/src/shimmers/math/geometry/collisions.cljc @@ -344,13 +344,17 @@ ;; FIXME shoudl this be iff, ie only the case if coincident-point is unique? (defmulti coincident-point? "Test if shapes `a` and `b` share a single point in common, either at a vertice or along - an intersecting edge." + an intersecting edge. Returns contact point if touching" (fn [a b] [(type a) (type b)])) +;; FIXME: This includes cases where polygons just intersect eachother (defmethod coincident-point? [Polygon2 Polygon2] [a b] - (some (fn [[ap bp]] (tm/delta= ap bp)) - (mc/cartesian-product (g/vertices a) (g/vertices b)))) + (or (some (fn [[ap bp]] (when (tm/delta= ap bp) ap)) + (mc/cartesian-product (g/vertices a) (g/vertices b))) + (some (fn [[a-edge b-edge]] + (intersect/segment-intersect a-edge b-edge)) + (mc/cartesian-product (g/edges a) (g/edges b))))) (defmulti adjacent? "Test if shapes `a` and `b` share a vertice or an edge, but do not intersect inside." diff --git a/test/shimmers/math/geometry/collisions_test.cljc b/test/shimmers/math/geometry/collisions_test.cljc index cfb6b3da..795627f8 100644 --- a/test/shimmers/math/geometry/collisions_test.cljc +++ b/test/shimmers/math/geometry/collisions_test.cljc @@ -8,7 +8,8 @@ [thi.ng.geom.line :as gl] [thi.ng.geom.polygon :as gp] [thi.ng.geom.rect :as rect] - [thi.ng.geom.vector :as gv])) + [thi.ng.geom.vector :as gv] + [thi.ng.math.core :as tm])) (deftest overlap? (t/testing "Rect2 Rect2" @@ -50,7 +51,11 @@ (deftest coincident-point? (t/testing "Polygon2 Polygon2" - (is (not (sut/coincident-point? (gp/polygon2 [0.1 0] [5 0.2] [2.5 3]) - (gp/polygon2 [0.0 0] [5 0.3] [2.5 -3])))) - (is (sut/coincident-point? (gp/polygon2 [0.1 0] [5 0.2] [2.5 3]) - (gp/polygon2 [0.0 0] [5 0.2] [2.5 -3]))))) + (is (nil? (sut/coincident-point? (gp/polygon2 [0.0 1] [5 2] [2.5 3]) + (gp/polygon2 [0.0 0] [5 0] [2.5 -3])))) + (is (tm/delta= (sut/coincident-point? (gp/polygon2 [0.1 0] [5 0.2] [2.5 3]) + (gp/polygon2 [0.0 0] [5 0.2] [2.5 -3])) + [5 0.2])) + (is (tm/delta= (sut/coincident-point? (gp/polygon2 [0 0] [10 0] [5 -5]) + (gp/polygon2 [5 0] [10 5] [0 5])) + [5 0]))))