TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Coder on September 18, 2019, 05:05:21 PM

Title: Remove item according to X factor
Post by: Coder 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))

Thank you in advance.
Title: Re: Remove item according to X factor
Post by: MP 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.
Title: Re: Remove item according to X factor
Post by: Coder 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.
Title: Re: Remove item according to X factor
Post by: Lee Mac 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. )
Title: Re: Remove item according to X factor
Post by: Coder on September 19, 2019, 07:49:40 AM
Another beautiful codes.
Thank you Lee for this clear example for me to understand.
Title: Re: Remove item according to X factor
Post by: BIGAL 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.