Author Topic: Sort a list of points  (Read 1290 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Sort a list of points
« 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))

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: Sort a list of points
« Reply #1 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)...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: Sort a list of points
« Reply #2 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...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Sort a list of points
« Reply #3 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)

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: Sort a list of points
« Reply #4 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:
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Sort a list of points
« Reply #5 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. )

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Sort a list of points
« Reply #6 on: June 17, 2021, 12:32:55 PM »
Marko, Roy,  all is OK.   Thank for your time.   :-)