From 208eb74ed6af4e016f34704bf7c7de547e8b1612 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Thu, 7 Jul 2022 08:12:31 -0500 Subject: [refactor] point containment, generalized [add] intersections --- src/utils.lisp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src/utils.lisp') diff --git a/src/utils.lisp b/src/utils.lisp index 7024a8d..165f29a 100644 --- a/src/utils.lisp +++ b/src/utils.lisp @@ -25,7 +25,7 @@ order, nil if not." (- (vec:vx c) (vec:vx a))))) -(defun intersectp (a b c d) +(defun segments-intersect-p (a b c d) "A B C and D are vectors of the sort created by 3d-vectors:vec, each representing a 2d point. Returns T if the line segment between A and B intersects the linesegment between C and D, NIL otherwise." @@ -33,6 +33,33 @@ and B intersects the linesegment between C and D, NIL otherwise." (and (not (eq (counterclockwisep a c d) (counterclockwisep b c d))) (not (eq (counterclockwisep a b c) (counterclockwisep a b d)))))) +(defun paths-intersect-p (path-a path-b) + "Paths are lists of vectors, each of which represents a 2d point." + (loop for (a1 a2 . more-a) on path-a + while a2 + thereis (loop for (b1 b2 . b-more) on path-b + while b2 + thereis (segments-intersect-p a1 a2 b1 b2)))) + +(defun path-contains-point (path pt) + "Path is a list of vectors, pt is a single vector." + (let* ((bounds + (path-bounds path)) + (corner + ;; creating a point guaranteed to be outside of the path + (vec:vec (- (getf bounds :left) (getf bounds :width)) + (- (getf bounds :bottom) (getf bounds :height)) + 0.0 1.0))) + (loop for (p1 p2 . more) on path + while p2 + when (segments-intersect-p p1 p2 pt corner) + count 1 into intersection-count + finally + (return (oddp intersection-count))))) + +(defun path-encloses-p (path other) + ) + (defun path-bounds (path) "Path is a list of vectors representing 2d points. Returns the bounds and width and height as a plist of the form -- cgit v1.2.3