Private Function Line_Intersect(ByVal L1_X1 As Single, ByVal L1_Y1 As Single, ByVal L1_X2 As Single, ByVal L1_Y2 As Single, _
ByVal L2_X1 As Single, ByVal L2_Y1 As Single, ByVal L2_X2 As Single, ByVal L2_Y2 As Single, _
ByRef Intersection As Vector) As Boolean
'Denominator for ua and ub are the same, so store this calculation
Dim Denominator As Double
Dim n_a As Double
Dim n_b As Double
Denominator = (L2_Y2 - L2_Y1) * (L1_X2 - L1_X1) - (L2_X2 - L2_X1) * (L1_Y2 - L1_Y1)
'n_a and n_b are calculated as seperate values for readability
' Dim n_a As Double =
'Dim n_b As Double =
'If ua >= 0D AndAlso ua <= 1D AndAlso ub >= 0D AndAlso ub <= 1D Then
' Intersection.X = L1.X1 + (ua * (L1.X2 - L1.X1))
' Intersection.Y = L1.Y1 + (ua * (L1.Y2 - L1.Y1))
' Return True
'End If
'n_a and n_b are calculated as seperate values for readability
n_a = (L2_X2 - L2_X1) * (L1_Y1 - L2_Y1) - (L2_Y2 - L2_Y1) * (L1_X1 - L2_X1)
n_b = (L1_X2 - L1_X1) * (L1_Y1 - L2_Y1) - (L1_Y2 - L1_Y1) * (L1_X1 - L2_X1)
' Make sure there is not a division by zero - this also indicates that
' the lines are parallel.
' If n_a and n_b were both equal to zero the lines would be on top of each
' other (coincidental). This check is not done because it is not
' necessary for this implementation (the parallel check accounts for this).
If Denominator = 0 Then
Line_Intersect = False
Exit Function
End If
' Calculate the intermediate fractional point that the lines potentially intersect.
Dim ua As Double: ua = n_a / Denominator
Dim ub As Double: ub = n_b / Denominator
' The fractional point will be between 0 and 1 inclusive if the lines
' intersect. If the fractional calculation is larger than 1 or smaller
' than 0 the lines would need to be longer to intersect.
If ua >= 0 And ua <= 1 And ub >= 0 And ub <= 1 Then
Intersection.X = L1_X1 + (ua * (L1_X2 - L1_X1))
Intersection.Y = L1_Y1 + (ua * (L1_Y2 - L1_Y1))
Line_Intersect = True
Exit Function
End If
Line_Intersect = False
End Function