TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: ribarm on June 22, 2020, 12:29:28 PM

Title: Sorting points by X,Y,Z coordinates...
Post by: ribarm on June 22, 2020, 12:29:28 PM
Hi there, I have 2 options from which I can't decide which one is more logical - better for usage in routine...
I want to sort points by X,Y,Z and I don't know how to choose right one :

Code: [Select]
(defun sortxyz ( pl )
  (setq pl
    (vl-sort pl
      (function
        (lambda ( a b )
          (if (= (car a) (car b))
            (if (= (cadr a) (cadr b))
              (< (caddr a) (caddr b))
              (< (cadr a) (cadr b))
            )
            (< (car a) (car b))
          )
        )
      )
    )
  )
)

Code: [Select]
(defun sortxyz ( pl )
  (setq pl
    (vl-sort pl
      (function
        (lambda ( a b )
          (if (= (caddr a) (caddr b))
            (if (= (cadr a) (cadr b))
              (< (car a) (car b))
              (< (cadr a) (cadr b))
            )
            (< (caddr a) (caddr b))
          )
        )
      )
    )
  )
)

So what is your opinion?
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: Lee Mac on June 22, 2020, 01:10:03 PM
It depends whether you want to sort by X, Y, Z or by Z, Y, X.
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: ribarm on June 22, 2020, 01:25:54 PM
It depends whether you want to sort by X, Y, Z or by Z, Y, X.

If you don't have that exact situation and it's all the same between those 2, which one would you choose?
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: MP on June 22, 2020, 01:57:23 PM
To add to Lee's comments -- frequently point sorting is based on grouping of some kind, lest the sorted points when graphed appear similar to a stock market graph -- which may or may not be the intended result.

For example -- points within a y ordinate threshold of each other might be considered a group, and then points within that group sorted on their x ordinates -- independent of other groups. Finally the groups would be sorted based on their median y values.

A real world example might be the insert points of text or attribute values that represent (fields within) a table structure of some kind -- e.g. a title block revision history. Sort (ascending) within a virtual row on x values; sort (descending) virtual rows based on median y values.

Or vice versa (columns) or both (matrix).

/2¢
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: ribarm on June 22, 2020, 02:07:01 PM
So I came to conclusion that IMHO, I want to sort from left to right, then from front to back (down-up) and then from bottom to top - this order like I stated - so by X,Y, and finally by Z... I think that this is the most common way of thinking - excluding arabic systems (right-left)... So which version mimic the most my system : first code or second one?
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: MP on June 22, 2020, 02:14:15 PM
For me a datum sort routine doesn't exist without a context; can't spec a generic one; sorry.
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: ribarm on June 22, 2020, 02:21:54 PM
I came to conclusion that the second one is the one that is the most like my system explained previously... Thanks all for attention, I'll use the second one in my routine...
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: MP on June 22, 2020, 02:36:40 PM
{blink} The second one looks more like it's sorting zyx.
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: ribarm on June 22, 2020, 02:44:26 PM
{blink} The second one looks more like it's sorting zyx.

It looks that way, but when you think what it does, it's just the opposite, just like I explained...

Anyway I'll use it nevertheless I am right or wrong, but this one is on my account...
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: Lee Mac on June 22, 2020, 03:53:50 PM
The second is definitely sorting z/y/x - consider that the z-coordinates of every pair of points are compared first and, if they differ, the points are sorted on this basis; only if the z-coordinates are equal does the function proceed to comparing the y-coordinate values and so on.
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: ribarm on June 22, 2020, 04:23:29 PM
The second is definitely sorting z/y/x - consider that the z-coordinates of every pair of points are compared first and, if they differ, the points are sorted on this basis; only if the z-coordinates are equal does the function proceed to comparing the y-coordinate values and so on.

Thanks for exhaustive explanation, Lee... Now, I suppose you are right now... Only thingy that bothered me is that long ago at cadtutor, you proposed second code... So usual way - if 2D points sorting is an issue it's compatible with first code and now it makes sense...

Code: [Select]
(vl-sort pl (function (lambda ( a b ) (if (= (car a) (car b)) (< (cadr a) (cadr b)) (< (car a) (car b))))))

Regards...
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: MP on June 22, 2020, 05:47:01 PM
I believe this will sort x|y|z:

Code: [Select]
(defun _sort-pl ( pl )
    (   (lambda ( f ) (vl-sort pl 'f))
        (lambda ( a b / m n ) (if (= (setq m (car a)) (setq n (car b))) (f (cdr a) (cdr b)) (< m n)))
    )
)
Title: Re: Sorting points by X,Y,Z coordinates...
Post by: myloveflyer on July 11, 2020, 09:58:57 PM
I believe this will sort x|y|z:

Code: [Select]
(defun _sort-pl ( pl )
    (   (lambda ( f ) (vl-sort pl 'f))
        (lambda ( a b / m n ) (if (= (setq m (car a)) (setq n (car b))) (f (cdr a) (cdr b)) (< m n)))
    )
)
Well done,Mp.