Skip to content

Commit

Permalink
Fix closest-point function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasanidas committed Jul 17, 2023
1 parent 7022777 commit 356498f
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/base/point.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -197,27 +197,36 @@ When using `:left-inserting` or `:right-inserting`, you must explicitly delete t
"Return the closest point on the POINT-LIST compare to POINT.
DIRECTION can be :up or :down depending on the desired point.
SAME-LINE if T the point in POINT-LIST can be in the same line as POINT."
(loop :for p :in point-list
:for flag := t :then nil
:with closest := nil
:do (progn
(when flag (setf closest p))

(when (or (and (eq direction :up)
(and (point> point p closest)
(or same-line
(not (same-line-p p point)))))
(and (eq direction :down)
(point< closest point p)
(or same-line
(not (same-line-p p point)))))

(setf closest p)))
:finally (return (and (or (and (eq direction :up)
(point> point closest))
(and (eq direction :down)
(point< point closest)))
closest ))))
(labels ((up (p closest)
(or (and (point>= point p closest)
(or same-line
(not (same-line-p p point))))
(point>= closest point)))
(down (p closest)
(or (and
(point> p point)
(<= (- (point-linum p) (point-linum point))
(- (point-linum closest) (point-linum point)))
(or same-line (not (same-line-p p point))))

(point<= closest point))))
(loop :for p :in point-list
:for flag := t :then nil
:with closest := nil
:do (progn
(when flag (setf closest p))
(when (and
(not flag)
(or (and (eq direction :up)
(up p closest))
(and (eq direction :down)
(down p closest))))
(setf closest p)))
:finally (return (and (or (and (eq direction :up)
(point> point closest))
(and (eq direction :down)
(point< point closest)))
closest)))))

(defun point-min (point &rest more-points)
(assert (%always-same-buffer point more-points))
Expand Down

0 comments on commit 356498f

Please sign in to comment.