明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 5118|回复: 4

[另类]使用API判断点是否在多边形中

[复制链接]
发表于 2004-1-8 20:42:00 | 显示全部楼层 |阅读模式

  1. Option Explicit

  2. Private Type COORD
  3.     x As Long
  4.     y As Long
  5. End Type
  6. Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
  7. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  8. Private Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long

  9. Const ALTERNATE = 1

  10. Private Function PointInPolyine(ByVal Pt As Variant, ByVal lwpLineObj As AcadLWPolyline) As Boolean
  11.    
  12.     ' 多段线顶点数目
  13.     Dim NumCoords As Long
  14.     NumCoords = (UBound(lwpLineObj.Coordinates) + 1) / 2
  15.    
  16.     ' 多段线顶点坐标
  17.     Dim poly() As COORD
  18.     ReDim poly(1 To NumCoords)
  19.     Dim i As Integer
  20.     For i = 0 To UBound(lwpLineObj.Coordinates) - 1 Step 2
  21.         poly(i / 2 + 1).x = lwpLineObj.Coordinates(i) * 10000
  22.         poly(i / 2 + 1).y = lwpLineObj.Coordinates(i + 1) * 10000
  23.     Next
  24.    
  25.     ' 创建区域
  26.     Dim hRgn As Long
  27.     hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
  28.     ' 如果成功创建
  29.     If hRgn Then
  30.         ' 如果点在区域内部
  31.         If PtInRegion(hRgn, Pt(0) * 10000, Pt(1) * 10000) <> 0 Then
  32.             PointInPolyine = True
  33.         End If
  34.     End If
  35.    
  36.     ' 删除区域
  37.     DeleteObject hRgn
  38. End Function

  39. Sub test()
  40.     Dim EntObj As AcadEntity
  41.     Dim pPt As Variant
  42.     ThisDrawing.Utility.GetEntity EntObj, pPt, "选择多段线: "
  43.     pPt = ThisDrawing.Utility.GetPoint(, "指定点: ")
  44.     If PointInPolyine(pPt, EntObj) Then
  45.         Debug.Print "点" & Round(pPt(0), 4) & "," & Round(pPt(1), 4) & "在多段线内部! "
  46.     Else
  47.         Debug.Print "点" & Round(pPt(0), 4) & "," & Round(pPt(1), 4) & "在多段线外部! "
  48.     End If
  49. End Sub

  50. 立即窗口
  51. 点234.1859,173.8979在多段线内部!
发表于 2004-1-8 22:30:00 | 显示全部楼层
点在多边形的边上无法判断哦?
发表于 2004-1-8 22:48:00 | 显示全部楼层
试验了一下,似乎只适用于凸多边形的情况!
发表于 2007-3-19 20:30:00 | 显示全部楼层

我怎么没有测试出来啊?

发表于 2008-3-29 19:58:00 | 显示全部楼层
研究一下efan2000API判断点是否在多边形中的用途.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-25 09:37 , Processed in 0.200309 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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