Author Topic: Sort list of points perpendicular to line  (Read 1210 times)

0 Members and 1 Guest are viewing this topic.

abe123456789

  • Newt
  • Posts: 24
Sort list of points perpendicular to line
« on: July 21, 2022, 09:09:38 PM »
How can I sort a list of points perpendicular to a line going towards the line? I found this code to sort points but does not give me control any help will be appreciated. I attached a drawing to illustrate what I am meaning.

Thank you,




Code: [Select]

(vl-sort ptlist
                    '(lambda (x y)
                       (if (= (car x) (car y))
                         (< (cadr x) (cadr y))
                         (< (car x) (car y)))
                    ))


dexus

  • Bull Frog
  • Posts: 208
Re: Sort list of points perpendicular to line
« Reply #1 on: July 22, 2022, 03:35:59 AM »
Maybe you can sort them by distance from the line.
So something along these lines:

Code - Auto/Visual Lisp: [Select]
  1. (setq target (car (entsel)))
  2. (vl-sort ptlist
  3.   (function (lambda (a b)
  4.     (<
  5.       (distance a (vlax-curve-getClosestPointTo target a))
  6.       (distance b (vlax-curve-getClosestPointTo target b))
  7.     )
  8.   ))
  9. )
For a lot of points you might want to cache the distances calculated in the list and sort on those so they don't have to be checked all the time.
« Last Edit: July 22, 2022, 03:39:15 AM by dexus »

abe123456789

  • Newt
  • Posts: 24
Re: Sort list of points perpendicular to line
« Reply #2 on: July 22, 2022, 11:24:57 AM »
lets say we got a list of points( 7 1 0) (6 1 0) (5 1 0) ( 4 1 0)

and we got a line with the start pt (8 3 0) and the line end pt ( 8 1 0)

How can i sort the list of points going towards the line?

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Sort list of points perpendicular to line
« Reply #3 on: July 22, 2022, 01:38:02 PM »
lets say we got a list of points( 7 1 0) (6 1 0) (5 1 0) ( 4 1 0)

and we got a line with the start pt (8 3 0) and the line end pt ( 8 1 0)

How can i sort the list of points going towards the line?
How do you want them sorted?
What if there are point on the right of the line?  (8 1 0) (9 1 0) (10 1 0) (11 1 0)
What if the line is reversed? from ( 8 1 0) to (8 3 0)
What if the line and the points are completely random?

ronjonp

  • Needs a day job
  • Posts: 7529

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

abe123456789

  • Newt
  • Posts: 24
Re: Sort list of points perpendicular to line
« Reply #5 on: July 22, 2022, 05:52:08 PM »
lets say we got a list of points( 7 1 0) (6 1 0) (5 1 0) ( 4 1 0)

and we got a line with the start pt (8 3 0) and the line end pt ( 8 1 0)

How can i sort the list of points going towards the line?
How do you want them sorted?
What if there are point on the right of the line?  (8 1 0) (9 1 0) (10 1 0) (11 1 0)
What if the line is reversed? from ( 8 1 0) to (8 3 0)
What if the line and the points are completely random?

I like the vl-sort i posted but i want to add the control where it goes in the direction of the line.

there wouldnt be point to the right of the line
the points wouldnt be random.

BIGAL

  • Swamp Rat
  • Posts: 1417
  • 40 + years of using Autocad
Re: Sort list of points perpendicular to line
« Reply #6 on: July 22, 2022, 10:07:41 PM »
If its points like in crossing parallel lines then yes dexus is correct use sort on distance but your list would be like ((dist x y)(dist x y) (dist x y) where dist is distance from a pt on line to new points. I use this method often. Maybe also closestpointto where perp is required.
A man who never made a mistake never made anything

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Sort list of points perpendicular to line
« Reply #7 on: July 23, 2022, 05:19:30 AM »
... i want to add the control where it goes in the direction of the line.

there wouldnt be point to the right of the line
the points wouldnt be random.

If there are no points on the right, then sorting by X is enough, maybe just changing "<" to ">", you first code should do it.
Sorry, but for me, "control where it goes in the direction of the line" has no meaning.

Better draw some points, draw a line, label the points in the order you want them. Repeat for other situations. Post a screenshot.



BIGAL

  • Swamp Rat
  • Posts: 1417
  • 40 + years of using Autocad
Re: Sort list of points perpendicular to line
« Reply #9 on: July 24, 2022, 01:33:57 AM »
The other thing to look at "Is it left or right" lee_mac has a routine that checks this so could make dist -ve for left and +ve for right sort should still work.
A man who never made a mistake never made anything