szx025 发表于 2023-8-17 08:20:54

坐标列表到列表外一点的最近点

(setq n 0)
    (while (and        (setq ss1 (nth n ns))   ;取得第n点的坐标
                (setq ww1 (vl-position ss1 ns))   ;取得第n点坐标在交点坐标列表中的位置
                (setq ss2 (nth (1+ ww1) ns))         ;取得第n点下一个点的坐标
                (setq is1 (distance ss1 pt3))               
                (setq is2 (distance ss2 pt3))      ;取得两个坐标点的距离
           )
          (if(<= is1 is2 )
          (setq pt ss1)
          (setq pt ss2)
          )
      (setq n (1+ n))
    )
NS是坐标列表 PT3是列表外一点,要找出NS中距PT3最近的一点,上面的写法在特定条件下是正确的,但大部分情况不对,请教一下正确的写法

liuhe 发表于 2023-8-17 08:45:36


(SETQ PT (nth 0 ns))
(FOREACH X (CDR NS)
(SETQ        IS1 (distance PT3 PT)
        IS2 (distance PT3 X)
)
(IF (>= is1 is2)
    (SETQ PT X)
)
)

lijiao 发表于 2023-8-17 09:06:50

(setq ns1 (mapcar '(lambda(x)
                     (cons (distance x pt3) x))
                  ns)
      dismin (apply 'min (mapcar 'car ns1))
      pt (cdr (assoc dismin ns1)))

szx025 发表于 2023-8-18 08:51:35

谢谢上面两位朋友的热心指导

花开富贵 发表于 2023-8-18 12:15:19

本帖最后由 花开富贵 于 2023-8-18 12:17 编辑


[*];;说明:计算列表中到指定点的最短距离以及此点坐标
[*];;参数:ptSpeci:指定点坐标
[*];;参数:ptLst:列表,成员为点坐标
[*];;返回:列表
[*](defun Point::Near(ptSpeci ptLst / discur ret dis0818)
[*];;(car (Point::Near '(1 1) '((0 0) (10 0) (10 10))))-->(0 0)
[*](mapcar'(lambda(x)
[*]       (setq discur(distance x ptSpeci))
[*]       (if(or(null dis0818)(< discur dis0818))(setq dis0818 discur ret (cons x discur))       )
[*]   )
[*]    ptLst)
[*]ret
[*])

花开富贵 发表于 2023-8-18 12:19:05

花开富贵 发表于 2023-8-18 12:15
[*];;说明:计算列表中到指定点的最短距离以及此点坐标
[*];;参数:ptSpeci:指定点坐标
[*];;参数:ptLst: ...

;;说明:计算列表中到指定点的最短距离以及此点坐标
;;参数:ptSpeci:指定点坐标
;;参数:ptLst:列表,成员为点坐标
;;返回:列表
(defun Point::Near(ptSpeci ptLst / discur ret dis0818)
        ;;(car (Point::Near '(1 1) '((0 0) (10 0) (10 10))))-->(0 0)
        (mapcar                 '(lambda(x)
                       (setq discur(distance x ptSpeci))
                       (if(or(null dis0818)(< discur dis0818))       (setq dis0818 discur ret (cons x discur)) )
               )
                ptLst)
        ret
)

bai2000 发表于 2024-2-17 10:00:12

楼上,我刚刷到,能举个实例么?
页: [1]
查看完整版本: 坐标列表到列表外一点的最近点