本帖最后由 dtucad 于 2023-9-11 23:13 编辑
利用向量求点到直线垂足的坐标,可用于三维
没啥技术含量,向量入门知识,希望对新手有用。
水平有限,如有错误,请不吝指正。
- ;利用向量求点到直线垂足的坐标
- ;参数1:直线起点、终点
- ;参数2:直线外一点
- ;返回值:点到直线垂足的坐标
- (defun footcoord (p1 p2 cp / dis dot-product foot projection v-p v-w)
- (setq v-w (mapcar '- p2 p1));向量p1-p2
- (setq v-p (mapcar '- cp p1));向量p1-cp
- ;计算点到直线的投影比例
- (setq dot-product (+ (* (car v-p) (car v-w)) (* (cadr v-p) (cadr v-w)) (* (caddr v-p) (caddr v-w))));点乘
- (setq dis (distance p1 p2))
- (setq projection (/ dot-product (expt dis 2)))
- (setq foot (list (+ (car p1) (* (car v-w) projection)) (+ (cadr p1) (* (cadr v-w) projection)) (+ (caddr p1) (* (caddr v-w) projection))));求垂足的坐标
- )
- ;测试用
- (defun c:tt (/ cp dis p1 p2 pt)
- (if (and
- (setq p1 (getpoint "\n指定直线起点"))
- (setq p2 (getpoint p1 "\n指定直线终点"))
- (setq cp (getpoint "\n指定直线外一点"))
- )
- (progn
- (setq pt (footcoord p1 p2 cp))
- (princ "\n点到直线的垂足坐标为:")(princ pt)
- )
- )
- (princ)
- )
|