明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: 自贡黄明儒

[讨论] 判断:一个点在封闭曲线内

[复制链接]
发表于 2024-3-29 16:36 | 显示全部楼层
(defun PtInPoly (/ clst apt delta ints oLine oPline pl pline someAngle)
  (vl-load-com)
  (cond
    ((setq pline (car (entsel "\nPolyline: ")))
     ;; NO error checking .. assume a lwpoly is selected!
     (setq oPline (vlax-ename->vla-object pline)
           ;; pick point to investigate
           aPt       (getpoint "\nPick point: ")
           ;; initialize starting angle for "ray"
           someAngle 0.0
           ;; set delta angle for rotation of "ray"
           delta     (/ (* 2 pi) 8)
     )
     (cond
       ;; start by making a "ray" shooting out from aPt
       ;; (should use vla-add but what the heck)
       ;; (should also use a real RAY and manipulate
       ;;  unit vector .. but what the heck)
       ((entmake (list '(0 . "LINE") (cons 10 aPt)
                       (cons 11 (polar aPt 0.0 100.0))
                 )
        )
        (setq oline (vlax-ename->vla-object (entlast)))
        (repeat 8
          (cond
            ;; get intersection points with pline and "ray"
            ((setq ints (vla-intersectwith oPline oLine acExtendNone))
             ;; should be using vlax-...-u-bounds and all that to check
             ;; safearray (but what the heck, this is quicker to write)
             (if (not (vl-catch-all-error-p (setq ints (vl-catch-all-apply
                                     'vlax-safearray->list
                                     (list (vlax-variant-value ints)))
                        )
                      )
                 )
               ;; just put nil for uneven number of hits and T for even
               (setq clst (cons (not (zerop (rem (length ints) 2.0))) clst))
             )
            )
          )
          ;; pause to see the "ray" move around
          (while (not (grread nil 10)))
          ;; move endpoint of "ray"
          (vla-put-endpoint oLine (vlax-3D-point
              (polar aPt (setq someAngle (+ delta someAngle)) 100.0)
            )
          )
        )
        (vla-delete oLine)
        (vlax-release-object oLine)
        (vlax-release-object oPline)
       )
     )
    )
  )
  ;; .. and a lazy decoding of result:
  (cond ((not (member 'nil clst))(princ "Inside"))
        ((not (member 'T clst))(princ "Outside"))
        ((princ "Probably on an edge or vertex")))
  (terpri)
  clst
)

info from:
https://www.theswamp.org/index.php?topic=1890.0
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-1 05:42 , Processed in 0.264535 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表