Jump to content

Recommended Posts

Posted (edited)

Thread%2002.jpg?dl=0

Hi All...

I am trying to build a lisp that can connect from point to point with "ssget" but it takes by point made earlier, how do I choose a point sequentially based on distance from first point? anyone have any idea?

 

Thanks in advance!

Edited by zamri_uut
Insert Image
Posted

Brute-force method = slow:

(defun c:ptjoin ( / i l m p s )
   (if (setq s (ssget '((0 . "POINT"))))
       (progn
           (repeat (setq i (sslength s))
               (setq l (cons (cdr (assoc 10 (entget (ssname s (setq i (1- i)))))) l))
           )
           (if (setq p (getpoint "\nPick 1st point: "))
               (progn
                   (setq p (trans p 1 0))
                   (while l
                       (setq p (closestpoint p l)
                             m (cons (cons 10 p) m)
                             l (vl-remove p l)
                       )
                   )
                   (entmake
                       (append
                           (list
                              '(000 . "LWPOLYLINE")
                              '(100 . "AcDbEntity")
                              '(100 . "AcDbPolyline")
                               (cons 90 (length m))
                              '(070 . 0)
                           )
                           m
                       )
                   )
               )
           )
       )
   )
   (princ)
)
(defun closestpoint ( p l / d q r x )
   (setq r (car l)
         d (distance p r)
   )
   (foreach q (cdr l)
       (if (< (setq x (distance p q)) d)
           (setq r q d x)
       )
   )
   r
)

Posted

Thank you Mr. Lee, that's what I expected. I will try to put into function that I created. Thank you again for your always willing to help. :D

Posted

Lee Sorting using pure lisp is a bit of a pain vl-sort helps but would lend itself to this, have a list (ptnum dist (x Y)) (1 23 (100 100)) (2 25 (23 23)) (3 35 (50 50)) so pt2 is closer than pt1 but pt3 is further away.

 

I did find this which is something I have been looking for something else and would sort the points very quick using this method this is easier than using while test.

 

; example from help
(vl-sort '((1 3) (2 2) (3 1))
            (function (lambda (e1 e2)
                        (< (cadr e1) (cadr e2)))))
((3 1) (2 2) (1 3))   

 

mytest

(setq lst (list (list 1 23 (list 100 100))(list 2 22 (list 50 100))(list 3 35 (list 50 50))))

(vl-sort lst
   (function (lambda (e1 e2)
   (< (cadr e1) (cadr e2)))))
; result
((2 22 (50 100)) (1 23 (100 100)) (3 35 (50 50)))

Posted

Note that there is no need to sort the points as we are only looking for the nearest point, and so sorting the remaining points in increasing order of distance from a given point is unnecessary.

 

Also note that such a sort would need to be performed for each point processed, since we are not sorting the points by distance from a single given point, but rather by distance from each successive point (greedy algorithm).

 

The entire task is essentially the Shortest Path Problem for which there are several well-known algorithms - note that the greedy algorithm implemented here will not necessarily yield the best result.

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