Closest point to a polyline segment

Closest point to a polyline segment

Browning_Zed
Advocate Advocate
464 Views
6 Replies
Message 1 of 7

Closest point to a polyline segment

Browning_Zed
Advocate
Advocate

Hi,
Autolisp has a function vlax-curve-getClosestPointTo that returns the point on a curve that is nearest to the specified point. I need the same method that can be used only for a part of a curve (polyline segment).
I can get the geometric parameters of a segment like this:

(setq
    pik (entsel)
    ent (car pik)
    gpp (fix
            (vlax-curve-getparamatpoint ent
                (vlax-curve-getclosestpointto
                    ent
                    (cadr pik)
                )
            )
        )
    pt1 (vlax-curve-getpointatparam ent gpp)
    pt2 (vlax-curve-getpointatparam ent (1+ gpp))
)

How can I get the closest point to the segment between points pt1 and pt2 without creating a new entity (line or polyline) from these two points?

0 Likes
Accepted solutions (1)
465 Views
6 Replies
Replies (6)
Message 2 of 7

Moshe-A
Mentor
Mentor

@Browning_Zed  hi,

 

Base on your code the closet point between pt1 and pt2 is the same closet point of the all curve as long as you do not change your pick point, to me it sound logic.

 

Moshe

 

 

 

0 Likes
Message 3 of 7

Browning_Zed
Advocate
Advocate

Sorry, I'll try to be more clear. What I mean is this.
If I make an imaginary line from pt1 to pt2, and then I specify a third point: 

(setq pt3 (getpoint))
Now how can I get the fourth point lying on the imaginary line (between pt1 and pt2) that is closest to pt3?

0 Likes
Message 4 of 7

calderg1000
Mentor
Mentor

Regards @Browning_Zed 

Try this code

;;;___
(defun c:test (/ pick ent gpp pt1 pt2 elin ptp pt12)
  (setq
    pik  (entsel)
    ent  (car pik)
    gpp  (fix
           (vlax-curve-getparamatpoint ent
                                       (vlax-curve-getclosestpointto
                                         ent
                                         (cadr pik)
                                       )
           )
         )
    pt1  (vlax-curve-getpointatparam ent gpp)
    pt2  (vlax-curve-getpointatparam ent (1+ gpp))
    elin (entmakex (list '(0 . "line")
                         (cons 10 pt1)
                         (cons 11 pt2)
                         (cons 60 1)
                   )
         )
  )
  (while
    (setq ptp
           (getpoint "\nSelect a point to find the point on the curve segment:")
    )
     (setq
       pt12 (vlax-curve-getclosestpointto
              elin
              ptp
            )
       a1   (angle '(0. 0. 0.) (mapcar '- pt1 pt2))
       a2   (angle '(0. 0. 0.) (mapcar '- ptp pt12))
     )
     (if (equal (abs (- a1 a2)) (* 0.5 pi) 0.001)
       (command "_point" "_non" pt12)
       (princ
         "\nThere is no point at the shortest distance to the Curve segment..."
       )
     )
  )
  (command "_erase" elin "")
)

Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes
Message 5 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

@Browning_Zed wrote:

.... 

(setq pt3 (getpoint))
Now how can I get the fourth point lying on the imaginary line (between pt1 and pt2) that is closest to pt3?

... without creating a new entity


(inters pt1 pt2 pt3 (polar pt3 (+ (angle pt1 pt2) (/ pi 2)) 1) nil)

Kent Cooper, AIA
0 Likes
Message 6 of 7

Sea-Haven
Mentor
Mentor

Using VLyou can use the function Interserctwith. Obj2 can be a temporary line.

 

(setq intpt (vlax-invoke obj 'intersectwith obj2 acextendthisentity))

 

 

0 Likes
Message 7 of 7

Browning_Zed
Advocate
Advocate

Thanks to all for the help. But as I said in the opening post, a solution was needed without creating a new entity. Kent1Cooper solution was the right one.

0 Likes