- 帖子
- 1018
- 主題
- 15
- 精華
- 0
- 積分
- 1058
- 點名
- 0
- 作業系統
- win7 32bit
- 軟體版本
- Office 2016 64-bit
- 閱讀權限
- 50
- 性別
- 男
- 來自
- 桃園
- 註冊時間
- 2012-5-9
- 最後登錄
- 2022-9-28
|
6#
發表於 2013-11-13 00:14
| 只看該作者
本帖最後由 stillfish00 於 2013-11-13 00:27 編輯
回復 4# ui123
他是C/C++寫的,改成VBA大約如下- Function pointInPoly(ptx As Double, pty As Double, arPoly) As Boolean
- Dim i As Long, j As Long
- Dim pix As Double, piy As Double
- Dim pjx As Double, pjy As Double
-
- For i = 1 To UBound(arPoly) - 1
- j = i + 1
- pix = arPoly(i, 1): piy = arPoly(i, 2)
- pjx = arPoly(j, 1): pjy = arPoly(j, 2)
-
- '不包含點在多邊形線上
- If Not (piy > pty) = (pjy > pty) Then
- If ptx < (pjx - pix) * (pty - piy) / (pjy - piy) + pix Then pointInPoly = Not pointInPoly
- End If
- Next
- End Function
複製代碼 利用他寫一個自訂函數RegionABC- Function RegionABC(x As Double, y As Double, RegionA As Range, RegionB As Range, RegionC As Range) As String
- '不包含點在ABC邊緣
- If pointInPoly(x, y, RegionA.Value) Then RegionABC = "A": Exit Function
- If pointInPoly(x, y, RegionB.Value) Then RegionABC = "B": Exit Function
- If pointInPoly(x, y, RegionC.Value) Then RegionABC = "C": Exit Function
- RegionABC = "不在ABC"
- End Function
複製代碼 先補齊B區的點,
使用,例如在O4公式打上 "=RegionABC(M4,N4,$C$4:$D$8,$C$10:$D$16,$C$18:$D$22)" |
|