Author Topic: Distance from point to line (without vlax)  (Read 2843 times)

0 Members and 1 Guest are viewing this topic.

AcoCar

  • Mosquito
  • Posts: 8
Distance from point to line (without vlax)
« on: September 28, 2021, 02:20:50 PM »
I am relatively new AutoLISP user, and i need some help to make routine for finding closest (perpendicular) distance from point to line, i know its easy with "vlax-curve-getClosestPointTo" command i did it that way, but i need to go around that, and make code without using 'vlax' functions, because to my knowledge MicrosurveyCAD 2014 doesn's support it (maybe i'm wrong, I will be glad if someone corrects me about this) but i tried using routine that I made, works perfectly with AutoCad and Brics....but it fails with MScad.
If someone could give me an idea how to work around this, i will be very grateful. :)

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Distance from point to line (without vlax)
« Reply #1 on: September 28, 2021, 03:49:02 PM »
If it's a line use the INTERS function combined with POLAR.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

d2010

  • Bull Frog
  • Posts: 326
Re: Distance from point to line (without vlax)
« Reply #2 on: September 28, 2021, 03:58:33 PM »
to my knowledge MicrosurveyCAD 2014 doesn's support it (maybe i'm wrong, I will be glad if someone corrects me about this) but i tried using routine that I made, works perfectly with AutoCad and Brics....but it fails with MScad.
If someone could give me an idea how to work around this, i will be very grateful. :)

Can the MicrosurveyCAD-2014, execute programs AutoLisp.R14 as .lsp?

AcoCar

  • Mosquito
  • Posts: 8
Re: Distance from point to line (without vlax)
« Reply #3 on: September 28, 2021, 05:18:46 PM »
If it's a line use the INTERS function combined with POLAR.

Thank you very much, works perfectly :D

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Distance from point to line (without vlax)
« Reply #4 on: September 28, 2021, 06:13:37 PM »
If it's a line use the INTERS function combined with POLAR.

Thank you very much, works perfectly :D

Glad to to help .. be aware that this only performs similar to getclosestpointto if the pick point is within the start and end points perpendicularly.
Code - Auto/Visual Lisp: [Select]
  1. (defun _cptl (e p / p1 p2 p3)
  2.   (if (and e p)
  3.     (progn (setq p1 (cdr (assoc 10 (entget e)))
  4.                  p2 (cdr (assoc 11 (entget e)))
  5.                  p3 (inters p (polar p (+ (angle p1 p2) (/ pi 2)) 1e-8) p1 p2 nil)
  6.            )
  7.            (grdraw p p3 3)
  8.     )
  9.   )
  10.   p3
  11. )
  12. (_cptl (car (entsel)) (getpoint))
« Last Edit: September 28, 2021, 10:57:25 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Distance from point to line (without vlax)
« Reply #5 on: September 28, 2021, 06:15:21 PM »
You could alternatively use my Project Point to Line function, and then use the standard AutoLISP distance function to calculate the distance between the original point and the projected point.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Distance from point to line (without vlax)
« Reply #6 on: September 28, 2021, 06:21:14 PM »
You could alternatively use my Project Point to Line function, and then use the standard AutoLISP distance function to calculate the distance between the original point and the projected point.

Very cool Lee  8-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Distance from point to line (without vlax)
« Reply #7 on: September 28, 2021, 07:23:11 PM »
Cheers Ron :-)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Distance from point to line (without vlax)
« Reply #8 on: September 29, 2021, 02:14:59 AM »
Hi,
Another way using vector calculus.
Code - Auto/Visual Lisp: [Select]
  1. ;; Returns the length of a vector
  2. (defun gc:VecLength (v) (distance '(0. 0. 0.) v))
  3.  
  4. ;; Returns the cross product of two vectors
  5. (defun gc:CrossProduct (v1 v2)
  6.   (list (- (* (cadr v1) (caddr v2)) (* (caddr v1) (cadr v2)))
  7.         (- (* (caddr v1) (car v2)) (* (car v1) (caddr v2)))
  8.         (- (* (car v1) (cadr v2)) (* (cadr v1) (car v2)))
  9.   )
  10. )
  11.  
  12. (defun distancePtLine (pt p1 p2 / v)
  13.   (setq v (mapcar '- p2 p1))
  14.   (/ (gc:VecLength (gc:CrossProduct v (mapcar '- pt p1)))
  15.      (gc:VecLength v)
  16.   )
  17. )
Speaking English as a French Frog

AcoCar

  • Mosquito
  • Posts: 8
Re: Distance from point to line (without vlax)
« Reply #9 on: September 29, 2021, 05:51:53 PM »
You could alternatively use my Project Point to Line function, and then use the standard AutoLISP distance function to calculate the distance between the original point and the projected point.

This looks very nice, i had specific problem in mind, input was x number of points where first two represent axis, and i need to measure closest distance of every other point to that axis. I solved it using combination of Inters and Polar functions, as Ron suggested, it seems almost funny now, but i have spent few days trying to figure it out. Thank you once again!

 

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: Distance from point to line (without vlax)
« Reply #10 on: September 30, 2021, 05:40:57 AM »
one more to the colleciton
Code: [Select]
(defun vk_GetPerpenLength (p1 p2 P / A B C)
  (setq A (distance p1 P)
B (distance p2 P)
C (distance p1 p2)
  )
  (sqrt (abs (- (expt A 2) (expt (/ (+ (- (expt A 2) (expt B 2)) (expt C 2)) (* 2 C)) 2))))
)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Distance from point to line (without vlax)
« Reply #11 on: September 30, 2021, 04:33:28 PM »
Nice use of Heron's formula VovKa  :-)

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: Distance from point to line (without vlax)
« Reply #12 on: September 30, 2021, 05:55:59 PM »
oh man, i thought i've invented it myself :(
joking
remember it from my geometry class back at school :)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Distance from point to line (without vlax)
« Reply #13 on: September 30, 2021, 06:12:25 PM »
oh man, i thought i've invented it myself :(

I'd have believed you :wink:

apricot125

  • Mosquito
  • Posts: 13
Re: Distance from point to line (without vlax)
« Reply #14 on: November 04, 2021, 09:19:18 PM »
Helen formula

Code: [Select]


(defun ClosedDis( / pt0 pt1 pt2 s disa disb disc ename edata area ans ang pt disx osmd)
  (setq osmd (getvar "osmode"))
  (setvar "osmode" 0)
  (Setq pt0 (getpoint "Select the point out of the line: ")
        ename (car (entsel "Select the line: "))
        edata (entget ename)
        pt1   (cdr (assoc 10 edata))
        pt2   (cdr (assoc 11 edata))
        disa  (distance pt1 pt2)
        disb  (distance pt0 pt1)
        disc  (distance pt0 pt2)
        s     (/ (+ disa disb disc) 2.0)
        area  (sqrt (* s (- s disa) (- s disb) (- s disc)))
        ans   (/ (* 2.0 area) disa)
        ang   (angle pt1 pt2)
        disx  (sqrt (- (* disb disb) (* ans ans)))
        pt    (polar pt1 ang disx)
  )
  ;(command "_line" pt0 pt "")
  (entmake (list '(0 . "LINE") (cons 10 pt0) (cons 11 pt) '(62 . 1)))
  (setvar "osmode" osmd)
  (princ)

   
 
)
« Last Edit: November 04, 2021, 10:00:04 PM by apricot125 »