Jump to content

How to check if a point is in a list ?


Recommended Posts

Posted (edited)

Hello .

 

How can I check if a point of coordinates is a member in a list of points ?

 (setq pts '((27.3814 14.1609 0.0) (24.6235 10.9329 0.0) (19.8956 13.6626 0.0) (17.6971 9.85463 0.0)))

(setq pt '(27.3814 14.1609 0.0))
(member pt pts)

 

Thanks in advance

Edited by Michaels
Forgot to type comman for the list of points
Posted
(defun C:test (/ pts pt)

(setq pts '((27.3814 14.1609 0.0) (24.6235 10.9329 0.0) (19.8956 13.6626 0.0) (17.6971 9.85463 0.0)))
(setq pt '(27.3814 14.1609 0.0))
(if (member pt pts)
 (princ "\n Point list found")
 (princ "\n Point list not found")
)
(princ)
)

Posted

Thank you so much . :)

 

My try was the same as yours , and I do wonder why it did not work for me at the beginning !!!

 

Cheers :beer:

Posted

Watch out for (member) calls in lists that contain REALs. There isn't a fuzz factor so the numbers must be exact matches. A lot depends on how the numbers were attained. -David

Posted

Good point David. I guess you could roll your own...

 

(defun _member (item lst)
 (if (car lst)
   (if (equal (car lst) item)
     lst
     (_member item (cdr lst))
   )
 )
)

Posted (edited)

Very elegant Alan :)

 

A few variations:

 

(defun _memberwithfuzz ( expr lst fuzz )
   (vl-member-if '(lambda ( x ) (equal x expr fuzz)) lst)
)

(defun _memberwithfuzz ( expr lst fuzz / x )
   (while (and (setq x (car lst)) (not (equal expr x fuzz)))
       (setq lst (cdr lst))
   )
   lst
)

(defun _memberwithfuzz ( expr lst fuzz / foo bar )
   (defun foo ( x ) (equal x expr fuzz))
   (defun bar ( x )
       (if (foo x) (progn (defun foo ( x ) (list x)) (list x)))
   )
   (apply 'append (mapcar 'bar lst))
)

Edited by Lee Mac
Posted

;;;SearchAtom List Fuzz
(defun memberfz (s l f / r)
(foreach a l
  (and (not r)
       (equal s a f)
       (setq r T)))
 r)

 

-David

Posted
Very elegant Alan :)

 

A few variations:

Thanks, you too.

 

 

 

Slight mod with definable fuzz factor...

 

(defun _memberFuzz (item lst fuzz)
 (if (car lst)
   (if (equal (car lst) item fuzz)
     lst
     (_member item (cdr lst) fuzz)
   )
 )
)

Posted

Thanks so much guys ,

 

I have lots of things to read .

 

Highly appreciated .

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...