TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Marc'Antonio Alessi on June 17, 2021, 10:56:19 AM

Title: Sort a list of points
Post by: Marc'Antonio Alessi on June 17, 2021, 10:56:19 AM
Problem: find the "pair of greater distances" and order the list of points so that it starts from the vertex between the two segments keeping the same ordering (in this case clockwise).
Edit: new condition > the two distances chosen must not be less than "x" (example 40)
For simplicity I have made an example with orthogonal polyline but it may not be...
Code: [Select]
Examples of list:
((  0.0  0.0 0.0) (  0.0 30.0 0.0) ( 15.0 30.0 0.0) ( 15.0 80.0 0.0) ( 35.0 80.0 0.0) ( 35.0 30.0 0.0) (105.0 30.0 0.0) (105.0  0.0 0.0))
((105.0  0.0 0.0) (  0.0  0.0 0.0) (  0.0 30.0 0.0) ( 15.0 30.0 0.0) ( 15.0 80.0 0.0) ( 35.0 80.0 0.0) ( 35.0 30.0 0.0) (105.0 30.0 0.0))
((105.0 30.0 0.0) (105.0  0.0 0.0) (  0.0  0.0 0.0) (  0.0 30.0 0.0) ( 15.0 30.0 0.0) ( 15.0 80.0 0.0) ( 35.0 80.0 0.0) ( 35.0 30.0 0.0))
(( 35.0 30.0 0.0) (105.0 30.0 0.0) (105.0  0.0 0.0) (  0.0  0.0 0.0) (  0.0 30.0 0.0) ( 15.0 30.0 0.0) ( 15.0 80.0 0.0) ( 35.0 80.0 0.0)) ;<<<<
(( 35.0 80.0 0.0) ( 35.0 30.0 0.0) (105.0 30.0 0.0) (105.0  0.0 0.0) (  0.0  0.0 0.0) (  0.0 30.0 0.0) ( 15.0 30.0 0.0) ( 15.0 80.0 0.0))
(( 15.0 80.0 0.0) ( 35.0 80.0 0.0) ( 35.0 30.0 0.0) (105.0 30.0 0.0) (105.0  0.0 0.0) (  0.0  0.0 0.0) (  0.0 30.0 0.0) ( 15.0 30.0 0.0))
(( 15.0 30.0 0.0) ( 15.0 80.0 0.0) ( 35.0 80.0 0.0) ( 35.0 30.0 0.0) (105.0 30.0 0.0) (105.0  0.0 0.0) (  0.0  0.0 0.0) (  0.0 30.0 0.0))
((  0.0 30.0 0.0) ( 15.0 30.0 0.0) ( 15.0 80.0 0.0) ( 35.0 80.0 0.0) ( 35.0 30.0 0.0) (105.0 30.0 0.0) (105.0  0.0 0.0) (  0.0  0.0 0.0))

Each list must give this result:
(( 35.0 30.0 0.0) (105.0 30.0 0.0) (105.0  0.0 0.0) (  0.0  0.0 0.0) (  0.0 30.0 0.0) ( 15.0 30.0 0.0) ( 15.0 80.0 0.0) ( 35.0 80.0 0.0))
Title: Re: Sort a list of points
Post by: ribarm on June 17, 2021, 11:08:27 AM
Marc'
(105+30) > (70+50)

According to your picture, you proposed wrong vertex for start (it should be IMHO LR - lower right point)...
Title: Re: Sort a list of points
Post by: ribarm on June 17, 2021, 11:19:35 AM
When I think twice, it could also be LL (lower left point) or 0,0,0 in your CS...
Title: Re: Sort a list of points
Post by: Marc'Antonio Alessi on June 17, 2021, 11:23:47 AM
Marc'
(105+30) > (70+50)

According to your picture, you proposed wrong vertex for start (it should be IMHO LR - lower right point)...
Thanks Marko, for  "pair of greater distances" I don't mean the sum but the highest pair of values (I don't know how to explain better) but maybe this is not possible ...

I think we need a further condition:
> that the two distances chosen are not less than x (example 40)
Title: Re: Sort a list of points
Post by: ribarm on June 17, 2021, 12:15:43 PM
I won!!!

Code: [Select]
(defun foo ( l / dl pairs pair pos p )
  (setq dl (mapcar '(lambda ( a b ) (distance a b)) l (append (cdr l) (list (car l)))))
  (setq pairs (mapcar '(lambda ( a b ) (list a b)) dl (append (cdr dl) (list (car dl)))))
  (setq pair (car (vl-sort (vl-remove-if '(lambda ( x ) (vl-some '(lambda ( y ) (< y 40.0)) x)) pairs) '(lambda ( a b ) (> (+ (car a) (cadr a)) (+ (car b) (cadr b)))))))
  (setq pos (vl-position pair pairs))
  (setq p (nth (1+ pos) (append l l)))
  (append (member p l) (reverse (cdr (member p (reverse l)))))
)

(princ (foo '((0.0 0.0 0.0) (0.0 30.0 0.0) (15.0 30.0 0.0) (15.0 80.0 0.0) (35.0 80.0 0.0) (35.0 30.0 0.0) (105.0 30.0 0.0) (105.0 0.0 0.0))))
(princ (foo '((105.0 0.0 0.0) (0.0 0.0 0.0) (0.0 30.0 0.0) (15.0 30.0 0.0) (15.0 80.0 0.0) (35.0 80.0 0.0) (35.0 30.0 0.0) (105.0 30.0 0.0))))
(princ (foo '((105.0 30.0 0.0) (105.0 0.0 0.0) (0.0 0.0 0.0) (0.0 30.0 0.0) (15.0 30.0 0.0) (15.0 80.0 0.0) (35.0 80.0 0.0) (35.0 30.0 0.0))))
(princ (foo '((35.0 30.0 0.0) (105.0 30.0 0.0) (105.0 0.0 0.0) (0.0 0.0 0.0) (0.0 30.0 0.0) (15.0 30.0 0.0) (15.0 80.0 0.0) (35.0 80.0 0.0))))
(princ (foo '((35.0 80.0 0.0) (35.0 30.0 0.0) (105.0 30.0 0.0) (105.0 0.0 0.0) (0.0 0.0 0.0) (0.0 30.0 0.0) (15.0 30.0 0.0) (15.0 80.0 0.0))))
(princ (foo '((15.0 80.0 0.0) (35.0 80.0 0.0) (35.0 30.0 0.0) (105.0 30.0 0.0) (105.0 0.0 0.0) (0.0 0.0 0.0) (0.0 30.0 0.0) (15.0 30.0 0.0))))
(princ (foo '((15.0 30.0 0.0) (15.0 80.0 0.0) (35.0 80.0 0.0) (35.0 30.0 0.0) (105.0 30.0 0.0) (105.0 0.0 0.0) (0.0 0.0 0.0) (0.0 30.0 0.0))))
(princ (foo '((0.0 30.0 0.0) (15.0 30.0 0.0) (15.0 80.0 0.0) (35.0 80.0 0.0) (35.0 30.0 0.0) (105.0 30.0 0.0) (105.0 0.0 0.0) (0.0 0.0 0.0))))

 :lol:
Title: Re: Sort a list of points
Post by: roy_043 on June 17, 2021, 12:16:40 PM
Code - Auto/Visual Lisp: [Select]
  1. (defun point-list-shift (lst minDist / foundDist foundPt)
  2.   (mapcar
  3.     '(lambda (prev cur next / distA distB)
  4.       (if
  5.         (and
  6.           (> (setq distA (distance prev cur)) minDist)
  7.           (> (setq distB (distance cur next)) minDist)
  8.           (> (+ distA distB) foundDist)
  9.         )
  10.         (setq
  11.           foundDist (+ distA distB)
  12.           foundPt   cur
  13.         )
  14.       )
  15.     )
  16.     (cons (last lst) lst)
  17.     lst
  18.     (append (cdr lst) (list (car lst)))
  19.   )
  20.   (append
  21.     (member foundPt lst)
  22.     (reverse (cdr (member foundPt (reverse lst))))
  23.   )
  24. )
Title: Re: Sort a list of points
Post by: Marc'Antonio Alessi on June 17, 2021, 12:32:55 PM
Marko, Roy,  all is OK.   Thank for your time.   :-)