Jump to content

Get closest point to new point from a list


Aftertouch

Recommended Posts

Hello everybody,

 

i have a list with coördinates called 'TESTLIST'

!testlist
((106213.0 507453.0 0.078) (106220.0 507452.0 0.12) (106229.0 507450.0 0.067) (106236.0 507449.0 0.082) (106242.0 507447.0 0.09) (106243.0 507447.0 0.096) (106244.0 507447.0 0.086) (106245.0 507446.0 0.088) (106251.0 507444.0 0.054) (106262.0 507440.0 0.113) (106272.0 507436.0 0.073) (106279.0 507433.0 0.151) (106288.0 507430.0 0.101) (106295.0 507427.0 0.176) (106303.0 507424.0 0.133) (106305.0 507423.0 0.133))

 

i have a coördinate:

(setq pt (getpoint))
click =>  (105810.0 507425.0 0.0)

 

How can i get the closest point to my 'pt', from the points in the list?

(setq closestpoint <point from list here>)

Link to comment
Share on other sites

(setq rtnpt (car (vl-sort testlist (function (lambda ( a b ) (< (distance pt a) (distance pt b)))))))

Link to comment
Share on other sites

Just a guess:

 

Using:

; Lee Mac
; http://www.cadtutor.net/forum/showthread.php?100467-Find-the-highest-and-lowest-mark-from-the-selected-quot-points-quot&p=683159
(defun extremum ( cmp lst / rtn ) 
 (setq rtn (car lst))
 (foreach itm (cdr lst) (if (apply cmp (list itm rtn)) (setq rtn itm)) )
 rtn
)

 

Then given a:

(setq p '(105810.0 507425.0 0.0))
(setq pL
 '((106213.0 507453.0 0.078) (106220.0 507452.0 0.12) (106229.0 507450.0 0.067) 
   (106236.0 507449.0 0.082) (106242.0 507447.0 0.09) (106243.0 507447.0 0.096) 
   (106244.0 507447.0 0.086) (106245.0 507446.0 0.088) (106251.0 507444.0 0.054) 
   (106262.0 507440.0 0.113) (106272.0 507436.0 0.073) (106279.0 507433.0 0.151) 
   (106288.0 507430.0 0.101) (106295.0 507427.0 0.176) (106303.0 507424.0 0.133) (106305.0 507423.0 0.133)
 )
)

 

Result is:

_$ (extremum 
 (function 
   (lambda (a b) 
     (< (apply '+ (mapcar 'abs (mapcar '- a p)))  (apply '+ (mapcar 'abs (mapcar '- b p))))
   )
 ) 
 pL
)
(106213.0 507453.0 0.078)
_$ 

 

 

And some more "clear" test:

(setq p '(0.0 0.0 0.0))
(setq pL
 '((1.0 1.0 30.0) (2.0 2.0 0.0) (3.0 3.0 0.0) 
   (4.0 4.0 0.0) (5.0 5.0 0.0) (6.0 6.0 0.0) 
 )
)

_$ (extremum 
 (function 
   (lambda (a b) 
     (< (apply '+ (mapcar 'abs (mapcar '- a p)))  (apply '+ (mapcar 'abs (mapcar '- b p))))
   )
 ) 
 pL
)
(2.0 2.0 0.0)

 

But I'm not 100% confident about this.

Link to comment
Share on other sites

In plain autolisp, it could look like this :

 

[b][color=BLACK]([/color][/b]defun c:mindistp [b][color=FUCHSIA]([/color][/b]/ lst fp p d[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq lst '[b][color=NAVY]([/color][/b][b][color=MAROON]([/color][/b]10 20 10[b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]12 20 8[b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]30 15 1[b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]40 24 6[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]initget 1[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq fp [b][color=NAVY]([/color][/b]getpoint [color=#2f4f4f]"\nTest Point:   "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]foreach p lst
     [b][color=NAVY]([/color][/b]cond [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]equal p fp 1e-8[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
           [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]not d[b][color=GREEN])[/color][/b]
            [b][color=GREEN]([/color][/b]setq d [b][color=BLUE]([/color][/b]distance fp p[b][color=BLUE])[/color][/b]
                  s p[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
           [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]< [b][color=BLUE]([/color][/b]distance fp p[b][color=BLUE])[/color][/b] d[b][color=GREEN])[/color][/b]
            [b][color=GREEN]([/color][/b]setq d [b][color=BLUE]([/color][/b]distance fp p[b][color=BLUE])[/color][/b]
                  s p[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]prin1 [b][color=NAVY]([/color][/b]list d s[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

 

-David

Edited by David Bethel
Test For Comparison Point Equal Find Point
Link to comment
Share on other sites

David's is the most efficient one, but mine although the slowest is the shortest one...

 

It's up to you what version you are to use...

Link to comment
Share on other sites

Another:

 

; (car (ClosestFurthestPt p pL))
(defun ClosestFurthestPt ( p pL / L )
 (setq L (mapcar (function (lambda (x) (distance p x))) pL))
 (mapcar (function (lambda (x) (nth (vl-position (apply x L) L) pL))) '(min max))
)

Link to comment
Share on other sites

Encountered a little problem:

 

I use this code, to get the closest point from 'testlist' to my current point 'pt' and set that point to 'rtnpt'....

 

(setq rtnpt (car (vl-sort testlist (function (lambda ( a b ) (< (distance pt a) (distance pt b)))))))

 

Sometimes, mt current point 'pt' is also in the testlist.... How can edit this code, so it checks for the closest point, and 'skips' if the found point is the current point...

Link to comment
Share on other sites

Updated post 4

 

Sometimes plain AutoLisp can be modified so much easier than Visual Lisp routines

 

My &0.02 -David

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...