Function IsPointInPolygon(X, Y, poly() AsDouble) AsBoolean ' Check if a point is inside a polygon. ' Args: ' x: X coordinate of the point. ' y: Y coordinate of the point. ' poly: An array representing the vertices of the polygon. ' Returns: ' True if the point is inside the polygon, False otherwise.
Dim i AsInteger, j AsInteger Dim numVertices AsInteger Dim oddNodes AsBoolean
numVertices = UBound(poly) \ 2 + 1
' Check if the point is inside the polygon using the ray-casting algorithm j = numVertices - 1 oddNodes = False For i = 0To numVertices - 1 If (poly(2 * i + 1) > Y) <> (poly(2 * j + 1) > Y) Then If (poly(2 * j + 1) - poly(2 * i + 1)) <> 0Then If X < (poly(2 * j) - poly(2 * i)) * (Y - poly(2 * i + 1)) / (poly(2 * j + 1) - poly(2 * i + 1)) + poly(2 * i) Then oddNodes = Not oddNodes EndIf EndIf EndIf j = i Next i
' Check if the point is exactly on a vertex or an edge For i = 0To numVertices - 1 j = (i + 1) Mod numVertices ' Check if the point is on a vertex If X = poly(2 * i) And Y = poly(2 * i + 1) Then IsPointInPolygon = True ExitFunction EndIf ' Check if the point is on an edge If ((poly(2 * i + 1) <= Y And Y <= poly(2 * j + 1)) Or (poly(2 * j + 1) <= Y And Y <= poly(2 * i + 1))) And _ (X <= myMax(poly(2 * i), poly(2 * j))) And (poly(2 * j + 1) - poly(2 * i + 1) <> 0) Then If X = (poly(2 * j) - poly(2 * i)) * (Y - poly(2 * i + 1)) / (poly(2 * j + 1) - poly(2 * i + 1)) + poly(2 * i) Then IsPointInPolygon = True ExitFunction EndIf EndIf Next i
Function PolygonsRelation(poly1() AsDouble, poly2() AsDouble) AsString ' Check relation between two closed polygons. ' Args: ' poly1: An array representing the vertices of the first closed polygon. ' poly2: An array representing the vertices of the second closed polygon. ' Returns: ' String indicating the relation between two polygons: ' - "Disjoint": If polygons are disjoint. ' - "Contains": If poly1 contains poly2. ' - "Contained By": If poly2 contains poly1. ' - "Overlap": If polygons overlap. Dim i AsInteger, j AsInteger Dim numVertices1 AsInteger, numVertices2 AsInteger Dim containsPoly1 AsBoolean, containsPoly2 AsBoolean Dim intersects AsBoolean Dim X AsDouble Dim Y AsDouble numVertices1 = UBound(poly1) \ 2 + 1 numVertices2 = UBound(poly2) \ 2 + 1 ' Check if one polygon is completely contained in the other containsPoly1 = False containsPoly2 = False For i = 0To numVertices1 - 1'檢查點資料是否在poly2裡面
If IsPointInPolygon(poly1(2 * i), poly1(2 * i + 1), poly2) Then containsPoly1 = True ExitFor EndIf Next i For j = 0To numVertices2 - 1'檢查點資料是否在poly1裡面
If IsPointInPolygon(poly2(2 * j), poly2(2 * j + 1), poly1) Then containsPoly2 = True ExitFor EndIf Next j ' Check if polygons are disjoint IfNot containsPoly1 AndNot containsPoly2 Then intersects = False For i = 0To numVertices1 - 1 For j = 0To numVertices2 - 1 If DoIntersect(poly1(2 * i), poly1(2 * i + 1), poly1((2 * ((i + 1) Mod numVertices1))), poly1((2 * ((i + 1) Mod numVertices1)) + 1), _ poly2(2 * j), poly2(2 * j + 1), poly2((2 * ((j + 1) Mod numVertices2))), poly2((2 * ((j + 1) Mod numVertices2)) + 1)) Then intersects = True ExitFor EndIf Next j If intersects ThenExitFor Next i IfNot intersects Then PolygonsRelation = "Disjoint" ExitFunction EndIf EndIf ' Check for containment If containsPoly1 And containsPoly2 Then PolygonsRelation = "Overlap" ElseIf containsPoly1 Then PolygonsRelation = "Contains" ElseIf containsPoly2 Then PolygonsRelation = "Contained By" Else PolygonsRelation = "Overlap" EndIf EndFunction