本帖最后由 yuanziyou 于 2017-10-6 20:44 编辑
用内置的曲线函数:- (defun c:fgg ();点到直线的距离
- (vl-load-com)
- (setq objen (car (entsel "请选择直线")))
- (setq pt2 (getpoint "\n指定点:"))
- (setq pt (vlax-curve-getClosestPointTo objen pt2 T))
- (setq d1 (rtos (distance pt pt2) 2 3))
- (setq d2 (rtos (distance pt (vlax-curve-getStartPoint objen)) 2 3))
- (princ "\n垂足到起点的距离D2为=") (princ d2) (princ)
- (princ "\n待求点到直线垂足的距离D1为=") (princ d1) (princ)
- )
因为我最近也在写关于点到多段线的,也可以用矢量法:
- (defun c:p2l ();点到直线的距离 (setq pt1 (getpoint "\n请选择直线起点:"))
- (setq pt2 (getpoint "\n请选择直线第二点:"))
- (setq pt3 (getpoint "\n请输入待求点:"))
- (setq x1 (car pt1));获取点列表的第一个元素
- (setq y1 (cadr pt1));获取点列表的第二个元素
- (setq x2 (car pt2));获取点列表的第一个元素
- (setq y2 (cadr pt2));获取点列表的第二个元素
- (setq x3 (car pt3));获取点列表的第一个元素
- (setq y3 (cadr pt3));获取点列表的第二个元素
- (setq d1 (P2LDist x1, y1, x2, y2, x3, y3)
- d3 (distance pt1 pt3)
- )
- (if (< (- d3 (abs d1)) 0.00001)
- (setq d2 0)
- (setq d2 (sqrt (- (* d3 d3) (* d1 d1))))
- )
- (princ "\n垂足到起点的距离D2为=") (princ (rtos d2 2 3)) (princ)
- (princ "\n待求点到直线垂足的距离D1为=") (princ (rtos (abs d1) 2 3)) (princ)
- )
- (defun P2LDist(x1, y1, x2, y2, x3, y3);点到直线的距离左+右-
- (setq r0(Sqrt (+ (* ( - y2 y1) ( - y2 y1)) (* (- x1 x2) (- x1 x2)))));直线起点到终点的距离
- (setq a1(-(* x1 y2) (* x3 (- y2 y1)) (* y3 (- x1 x2)) (* x2 y1)))
- (setq distL (/ a1 r0))
- )
|