Jump to content

Recommended Posts

Posted (edited)

Hi all guys,

I need a function to find the point that has minimum Y in a list, like this :

((10 15) (19 25) (26 10) (50 32))

>> (26 10)

Can some one give me a function or best way to do this?

Thanks in advanced.

Edited by amir0914
Posted

Try. I can't test as I'm away from AutoCAD

 

(defun miny (lst) (car (vl-sort lst '(lambda (x y) (< (cadr x) (cadr y))))))

;example call (setq ymin (miny '((10 15) (19 25) (26 10) (50 32))))

 

  • Like 1
Posted

When finding extrema, it is typically more efficient to iterate over the list once, rather than using a sorting function (such as vl-sort) which necessarily performs many more comparisons to order the entire list - this is one circumstance where shorter code is not necessarily more efficient. You may find this thread of interest in this regard.

  • Like 1
Posted
2 hours ago, amir0914 said:
Quote

 

Hi all guys,

I need a function to find the point that has minimum Y in a list, like this :

((10 15) (19 25) (26 10) (50 32))

>> (26 10)

Can some one give me a function or best way to do this?

Thanks in advanced.

 

I won't probably iterate. I'll do the below, though I'm not sure if it's more efficient:

 

(defun findmin (lst / m)
  (setq m (apply 'min (mapcar 'cadr lst)))
  (car (vl-member-if '(lambda (x) (eq (cadr x) m)) lst))
  )

 

  • Like 1
Posted
9 hours ago, Lee Mac said:

When finding extrema, it is typically more efficient to iterate over the list once, rather than using a sorting function (such as vl-sort) which necessarily performs many more comparisons to order the entire list - this is one circumstance where shorter code is not necessarily more efficient. You may find this thread of interest in this regard.

You are right lee mac, I looked at the post that you implied. it's the exact post that i need.

 

Posted
(defun car-sort ( l f / removenth r k )

  (defun removenth ( l n / k )
    (setq k -1)
    (vl-remove-if '(lambda ( x ) (= (setq k (1+ k)) n)) l)
  )

  (setq k -1)
  (vl-some '(lambda ( a ) (setq k (1+ k)) (if (vl-every '(lambda ( x ) (apply f (list a x))) (removenth l k)) (setq r a))) l)
  r
)

;;; (car-sort '(2 4 1 3 5 1) '<) => nil
;;; (car-sort '(2 4 1 3 5 1) '<=) => 1

(setq yminpt (car-sort ptlst '(lambda ( a b ) (<= (cadr a) (cadr b)))))

 

Lee is right as usual... Consider using my (car-sort) function - should be fine with large lists, but be aware that when specifying sorting function '(lambda ( a b ) ... ) you don't use arguments the same as in (car-sort)... For ex. '(lambda ( x y ) ... ) will fail... Also, note that for comparisons you must use <= or >= instead like normal (vl-sort) < or > as like you see in example (car-sort) can't determine (< 1 1) as T and therefore return will be nil... (car-sort) will iterate just once and won't make further sorting of other elements - it will return just (car) element...

Regards, M.R.

  • Like 1

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...