Aftertouch Posted May 18, 2017 Share Posted May 18, 2017 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>) Quote Link to comment Share on other sites More sharing options...
marko_ribar Posted May 18, 2017 Share Posted May 18, 2017 (setq rtnpt (car (vl-sort testlist (function (lambda ( a b ) (< (distance pt a) (distance pt b))))))) Quote Link to comment Share on other sites More sharing options...
Grrr Posted May 18, 2017 Share Posted May 18, 2017 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. Quote Link to comment Share on other sites More sharing options...
David Bethel Posted May 18, 2017 Share Posted May 18, 2017 (edited) 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 May 23, 2017 by David Bethel Test For Comparison Point Equal Find Point Quote Link to comment Share on other sites More sharing options...
marko_ribar Posted May 18, 2017 Share Posted May 18, 2017 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... Quote Link to comment Share on other sites More sharing options...
Grrr Posted May 18, 2017 Share Posted May 18, 2017 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)) ) Quote Link to comment Share on other sites More sharing options...
Aftertouch Posted May 18, 2017 Author Share Posted May 18, 2017 Thanks all, I used Marko's code. Did the job for me. And is very fast! Thanks for the help! Quote Link to comment Share on other sites More sharing options...
Aftertouch Posted May 23, 2017 Author Share Posted May 23, 2017 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... Quote Link to comment Share on other sites More sharing options...
David Bethel Posted May 23, 2017 Share Posted May 23, 2017 Updated post 4 Sometimes plain AutoLisp can be modified so much easier than Visual Lisp routines My &0.02 -David Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.