TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: abe123456789 on July 21, 2022, 09:09:38 PM

Title: Sort list of points perpendicular to line
Post by: abe123456789 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)))
                    ))

Title: Re: Sort list of points perpendicular to line
Post by: dexus 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.
Title: Re: Sort list of points perpendicular to line
Post by: abe123456789 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?
Title: Re: Sort list of points perpendicular to line
Post by: Stefan 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?
Title: Re: Sort list of points perpendicular to line
Post by: ronjonp on July 22, 2022, 02:06:54 PM
Maybe this?
https://www.cadtutor.net/forum/topic/66657-perpendicular-lines-from-many-points-to-a-polyline/#comment-546306
Title: Re: Sort list of points perpendicular to line
Post by: abe123456789 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.
Title: Re: Sort list of points perpendicular to line
Post by: BIGAL 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.
Title: Re: Sort list of points perpendicular to line
Post by: Stefan 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.

Title: Re: Sort list of points perpendicular to line
Post by: HOSNEYALAA on July 23, 2022, 07:52:44 AM


https://www.cadtutor.net/forum/topic/33845-lisp-multi-level-sorting/?fbclid=IwAR2Dn1YVrDL-AykmSAH_qGszOLZ6he6ZnmtPMMZxy4_5QRyGjF5LETJqS64
Title: Re: Sort list of points perpendicular to line
Post by: BIGAL 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.