vanowm Posted January 23, 2016 Posted January 23, 2016 Hello. Can someone suggest a way to sort a list of selected entities by the shortest distance between end coordinate of one entity and start coordinate of the next? Here is an example: yellow points represent the start of entity, and the numbers is the order they should end up. #1 line is the closest to 0,0; it's end coordinate is closest to arch #2, then polyline #3 and so on. Thank you. P.S. What I'm trying to achieve here is optimize how my plotter draws. It's driver fails to optimize the way it draws, which seems to obey the sequence in which the entities were created in the drawing. Right now I have to manually copy each entity one by one in proper sequence the way I want it to plot... Quote
Lee Mac Posted January 24, 2016 Posted January 24, 2016 The problem is essentially the Travelling Salesman Problem (i.e. visiting all nodes following the shortest path). Below is a solution implementing a nearest-neighbour greedy algorithm - note that this approach will not yield the optimal result: (defun sortentlist ( lst / foo rtn ) (setq foo (lambda ( x l / a d r ) (setq d (distance (caddr x) (cadar l)) r (car l) ) (foreach y (cdr l) (if (< (setq a (distance (cadr y) (caddr x))) d) (setq d a r y) ) ) r ) ) (setq lst (mapcar '(lambda ( x ) (list x (vlax-curve-getstartpoint x) (vlax-curve-getendpoint x))) lst) rtn (list (car lst)) lst (cdr lst) ) (while lst (setq rtn (cons (foo (car rtn) lst) rtn) lst (vl-remove (car rtn) lst) ) ) (reverse (mapcar 'car rtn)) ) The algorithm could also be implemented using sorting, but this will be less efficient: (defun sortentlist ( lst / rtn ) (setq lst (mapcar '(lambda ( x ) (list x (vlax-curve-getstartpoint x) (vlax-curve-getendpoint x))) lst) rtn (list (car lst)) lst (cdr lst) ) (while lst (setq lst (vl-sort lst '(lambda ( a b ) (< (distance (caddar rtn) (cadr a)) (distance (caddar rtn) (cadr b))))) rtn (cons (car lst) rtn) lst (vl-remove (car rtn) lst) ) ) (reverse (mapcar 'car rtn)) ) Quote
BIGAL Posted January 24, 2016 Posted January 24, 2016 Lee was this not another post problem how to make the laser cutter do the next closest item saving head movement time. Quote
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.