;; | ---------------------------------------------------------------------------- ;; | GE_NearPt ;; | ---------------------------------------------------------------------------- ;; | Function : Locate a point nearest to a list of points ;; | Argument : [pt] - Point to which the nearest point is to be found out ;; | [vlist] - List of points ;; | Description : The function works in two passes. In the first pass, a ;; | perpendicular is dropped to each segment and the nearest point ;; | where the perpdiclar actually (and not by line extension) meets ;; | the segment is identified . The nearest point and the nearest ;; | distances are recorded. In the second pass, the point is compared ;; | against each point in the points list to see if any point is ;; | nearer than the "perpendicualr dropped nearest" as found during ;; | the first pass. If so, this point is taken as the absolute nearest. ;; | Return : The actual nearest point, maybe a perpendicular drop or a ;; | nearer point. The point is guaranteed to lie on the segment. ;; | e-mail : rakesh.rao@4d-technologies.com ;; | Web : www.4d-technologies.com ;; | ---------------------------------------------------------------------------- (defun GE_NearPt( pt vlist / tmp len p1 p2 cnt NearPt NearDist _NearPt _NearDist _pt OS ) (setq len (length vlist) cnt 0 _NearPt nil _NearDist 1E20 OS (getvar "OSMODE") ) (setvar "OSMODE" 0) (repeat (1- len) (setq p1 (nth cnt vlist) p2 (nth (1+ cnt) vlist) cnt (1+ cnt) ) (if (> (distance p1 p2) 0.001) (progn (command "._Ucs" "_3Point" p1 p2 (polar p1 (+ (angle p1 p2) #pb2) 1.0)) (setq _pt (trans pt 0 1) p1 (list 0.0 0.0 0.0) p2 (trans p2 0 1) ) (if (MI_InBetween (car _pt) 0.0 (car p2)) (progn (setq NearDist (abs (cadr _pt))) (if (< NearDist _NearDist) (setq _NearDist NearDist _NearPt (list (car _pt) 0.0 (caddr _pt)) _NearPt (trans _NearPt 1 0) )) )) (command "._Ucs" "_Previous") )) ) (foreach _pt vlist (if (< (setq tmp (distance pt _pt)) _NearDist) (setq _NearDist tmp _NearPt _pt ) ) ) (setvar "OSMODE" OS) _NearPt )