Type Point2D x As Single y As Single End Type Type Edge2D p1 As Point2D p2 As Point2D End Type Type Point3D x As Single y As Single z As Single End Type Type Edge3D p1 As Point3D p2 As Point3D End Type Type AVec2D 'Applied vector (wrench) in 2D p As Point2D x As Single y As Single End Type Function ccw(p0 As Point2D, p1 As Point2D, p2 As Point2D) As Integer 'This function (in Pascal) can be found on page 350 of Sedgewick's 'second edition of "Algorithms" 'Given three points in a path, report direction of turn: dx1 = p1.x - p0.x dy1 = p1.y - p0.y dx2 = p2.x - p0.x dy2 = p2.y - p0.y If dx1 * dy2 > dy1 * dx2 Then ccw = 1 If dx1 * dy2 < dy1 * dx2 Then ccw = -1 If dx1 * dy2 = dy1 * dx2 Then If dx1 * dx2 < 0 Or dy1 * dy2 < 0 Then ccw = -1 Else If dx1 * dx1 + dy1 * dy1 >= dx2 * dx2 + dy2 * dy2 Then ccw = 0 Else ccw = 1 End If End If End If End Function 'Projected facet on the box virtual wall: Type Patch p As Point3D 'The vertices of the patch n As Integer 'The number of vertices in the patch polygon rp As Point3D 'Reference point on the facet dx As Single 'Components of the direction vector normal to facet dy As Single dz As Single End Type Function Intersect(l1 As Edge2D, l2 As Edge2D) As Integer 'Sedgewick's algorithm is modified to accommodate VB boolean conventions: Temp1% = ccw(l1.p1, l1.p2, l2.p1) * ccw(l1.p1, l1.p2, l2.p2) If Temp1% <= 0 Then 'modified to work with "inside" Temp1% = -1 Else Temp1% = 0 End If Temp2% = ccw(l2.p1, l2.p2, l1.p1) * ccw(l2.p1, l2.p2, l1.p2) If Temp2% <= 0 Then Temp2% = -1 Else Temp2% = 0 End If If Temp1% And Temp2% Then Intersect = -1 Else Intersect = 0 End If End Function Function inside(t As Point2D, TestPatch() As Patch) As Integer 'From Sedgewick, p354: Dim lt As Edge2D Dim lp As Edge2D ReDim p(TestPatch(1).n + 1) As Point2D 'Find the mimimum x of the minimum y to anchor the polygon: MinY! = TestPatch(1).p.y MinXofMinY! = TestPatch(1).p.x MinIndex% = 1 For i% = 2 To TestPatch(1).n If TestPatch(i%).p.y <= MinY! Then If TestPatch(i%).p.y = MinY! Then If TestPatch(i%).p.x < MinXofMinY! Then MinXofMinY! = TestPatch(i%).p.x MinIndex% = i% End If Else MinY! = TestPatch(i%).p.y MinXofMinY! = TestPatch(i%).p.x MinIndex% = i% End If End If Next i% 'Reorder the polygon to put the min x of the min y as the anchor: For i% = 1 To TestPatch(1).n p(i%).x = TestPatch((MinIndex% + i% - 2 + TestPatch(1).n) Mod TestPatch(1).n + 1).p.x p(i%).y = TestPatch((MinIndex% + i% - 2 + TestPatch(1).n) Mod TestPatch(1).n + 1).p.y Next i% p(0) = p(TestPatch(1).n) p(n + 1) = p(1) lt.p1 = t lt.p2 = t lt.p2.x = 100 Count% = 0 j% = 0 For i% = 1 To TestPatch(1).n lp.p1 = p(i%) lp.p2 = p(i%) If Not Intersect(lp, lt) Then lp.p2 = p(j%) j% = i% If Intersect(lp, lt) Then Count% = Count% + 1 End If End If Next i% If Count% Mod 2 = 1 Then inside = -1 Else inside = 0 End If End Function