Author Topic: Remove item according to X factor  (Read 778 times)

0 Members and 1 Guest are viewing this topic.

Coder

• Swamp Rat
• Posts: 798
Remove item according to X factor
« on: September 18, 2019, 05:05:21 PM »
Hello guys.

How can I remove a list of coordinates if X coordinate is already matched ?

'((3934.9 1322.47 -1.0e-08) (3934.3 1322.47 -1.0e-08) (3934.9 1323.67 -1.0e-08) (3934.3 1323.67 -1.0e-08) (3931.03 1323.08 0.0) (3937.75 1323.08 0.0))

Final result must be like this:

'((3934.9 1322.47 -1.0e-08) (3934.3 1322.47 -1.0e-08) (3931.03 1323.08 0.0) (3937.75 1323.08 0.0))

MP

• Seagull
• Posts: 17665
• Have thousands of dwgs to process? Contact me.
Re: Remove item according to X factor
« Reply #1 on: September 18, 2019, 10:53:36 PM »
If I understood correctly here's a recursive one I quickly knocked out that employs a fuzz factor since you're dealing with real (imprecise) numbers:

Code: [Select]
`(defun foo ( pts fuzz / i x )    (if (setq x (caar pts) i (car pts))        (cons i (foo (vl-remove-if '(lambda (p) (equal x (car p) fuzz)) (cdr pts)) fuzz))    ))`
Test:

(foo
'(   (3934.9  1322.47 -1.0e-08)
(3934.3  1322.47 -1.0e-08)
(3934.9  1323.67 -1.0e-08)
(3934.3  1323.67 -1.0e-08)
(3931.03 1323.08  0.0)
(3937.75 1323.08  0.0)
)
0.01 ;; don't forget the fuzz
)

Result:

(
(3934.9  1322.47 -1.0e-08)
(3934.3  1322.47 -1.0e-08)
(3931.03 1323.08  0.0)
(3937.75 1323.08  0.0)
)

FWIW ... cheers.
Engineering Technologist • CAD Specialist • Programmer Analyst
Design • Drafting • Document Control • Automation.

Coder

• Swamp Rat
• Posts: 798
Re: Remove item according to X factor
« Reply #2 on: September 19, 2019, 02:31:38 AM »
Beautiful. Thank you MP.

Is there any other way to go with like foreach or even mapcar to get the same result than recursive ? since recursive is very difficult to me to understand at the meantime.

Lee Mac

• Seagull
• Posts: 12390
• London, England
Re: Remove item according to X factor
« Reply #3 on: September 19, 2019, 07:18:09 AM »
Here's an iterative variant -
Code - Auto/Visual Lisp: [Select]
1. (defun foo ( l f / r )
2.     (foreach x l
3.         (or (vl-some '(lambda ( y ) (equal (car x) (car y) f)) r)
4.             (setq r (cons x r))
5.         )
6.     )
7.     (reverse r)
8. )

Coder

• Swamp Rat
• Posts: 798
Re: Remove item according to X factor
« Reply #4 on: September 19, 2019, 07:49:40 AM »
Another beautiful codes.
Thank you Lee for this clear example for me to understand.

BIGAL

• Bull Frog
• Posts: 473
• 30 + years of using Autocad
Re: Remove item according to X factor
« Reply #5 on: September 22, 2019, 01:32:38 AM »
Another alternative not as good as lee's is to vl-sort the lst then you just compare nth item pairs.