Author Topic: Sorting points by X,Y,Z coordinates...  (Read 239 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Water Moccasin
  • Posts: 2357
  • Marko Ribar, architect
Sorting points by X,Y,Z coordinates...
« 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?
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12379
  • London, England
Re: Sorting points by X,Y,Z coordinates...
« Reply #1 on: June 22, 2020, 01:10:03 PM »
It depends whether you want to sort by X, Y, Z or by Z, Y, X.

ribarm

  • Water Moccasin
  • Posts: 2357
  • Marko Ribar, architect
Re: Sorting points by X,Y,Z coordinates...
« Reply #2 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?
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

MP

  • Seagull
  • Posts: 17653
  • Have thousands of dwgs to process? Contact me.
Re: Sorting points by X,Y,Z coordinates...
« Reply #3 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
Engineering Technologist Programmer Analyst CAD Specialist
Multi-Discipline Design Drafting Document Control Automation.
cadanalyst@gmail.com www.linkedin.com/in/cadanalyst

ribarm

  • Water Moccasin
  • Posts: 2357
  • Marko Ribar, architect
Re: Sorting points by X,Y,Z coordinates...
« Reply #4 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 back to front (up-down) and then from top to bottom - 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?
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

MP

  • Seagull
  • Posts: 17653
  • Have thousands of dwgs to process? Contact me.
Re: Sorting points by X,Y,Z coordinates...
« Reply #5 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.
Engineering Technologist Programmer Analyst CAD Specialist
Multi-Discipline Design Drafting Document Control Automation.
cadanalyst@gmail.com www.linkedin.com/in/cadanalyst

ribarm

  • Water Moccasin
  • Posts: 2357
  • Marko Ribar, architect
Re: Sorting points by X,Y,Z coordinates...
« Reply #6 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...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

MP

  • Seagull
  • Posts: 17653
  • Have thousands of dwgs to process? Contact me.
Re: Sorting points by X,Y,Z coordinates...
« Reply #7 on: June 22, 2020, 02:36:40 PM »
{blink} The second one looks more like it's sorting zyx.
Engineering Technologist Programmer Analyst CAD Specialist
Multi-Discipline Design Drafting Document Control Automation.
cadanalyst@gmail.com www.linkedin.com/in/cadanalyst

ribarm

  • Water Moccasin
  • Posts: 2357
  • Marko Ribar, architect
Re: Sorting points by X,Y,Z coordinates...
« Reply #8 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...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12379
  • London, England
Re: Sorting points by X,Y,Z coordinates...
« Reply #9 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.

ribarm

  • Water Moccasin
  • Posts: 2357
  • Marko Ribar, architect
Re: Sorting points by X,Y,Z coordinates...
« Reply #10 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...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

MP

  • Seagull
  • Posts: 17653
  • Have thousands of dwgs to process? Contact me.
Re: Sorting points by X,Y,Z coordinates...
« Reply #11 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)))
    )
)
Engineering Technologist Programmer Analyst CAD Specialist
Multi-Discipline Design Drafting Document Control Automation.
cadanalyst@gmail.com www.linkedin.com/in/cadanalyst