Skip to content

Commit

Permalink
first steps at ensuring coincident-point? works for edge intersections
Browse files Browse the repository at this point in the history
  • Loading branch information
dgtized committed May 29, 2024
1 parent 7f45ca1 commit b38254b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/shimmers/math/geometry/collisions.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
15 changes: 10 additions & 5 deletions test/shimmers/math/geometry/collisions_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]))))

0 comments on commit b38254b

Please sign in to comment.