Nick.
I took your code and added the following.
1) Return boolean as to whether or not the line segments actually cross
2) If lines are parallel or coincident return false, thus x,y can be ignored
3) Otherwise x,y are the point of intersection (where they do or would intersect)
code kept simple to be less confusing, can be collapsed to be more efficient less readable.
Public Function Intersection(Line1 As LineSegment, Line2 As LineSegment) As Intersect
'if Line1 and Line2 intersect, return Intersect as True and the X,Y point of intersection
'otherwise, they are parallel, coincident or do not intesect, return Intersect as False
Dim Slope1 As Double
Dim Slope2 As Double
Dim YInt1 As Double
Dim YInt2 As Double
Dim Line1Vertical As Boolean
Dim Line2Vertical As Boolean
Dim Line1Horizontal As Boolean
Dim Line2Horizontal As Boolean
'rule out simple cases
If Line1.x1 = Line1.x2 Then Line1Vertical = True
If Line2.x1 = Line2.x2 Then Line2Vertical = True
If Line1.y1 = Line1.y2 Then Line1Horizontal = True
If Line2.y1 = Line2.y2 Then Line2Horizontal = True
If Line1Vertical And Line2Vertical Or Line1Horizontal And Line2Horizontal Then Exit Function 'false
'Get the slopes, handle simple case of eiher line vertical - both cannot due to above cases
If Line1Vertical Then
'Calculate X and Y as if they intersect
Slope2 = (Line2.y2 - Line2.y1) / (Line2.x2 - Line2.x1)
Intersection.x = Line1.x1
Intersection.y = Slope2 * Line1.x1 - (Slope2 * Line2.x1 - Line2.y1)
'does x,y lie on both lines?
If Between(Intersection.x, Line1.x1, Line1.x2) And _
Between(Intersection.x, Line2.x1, Line2.x2) And _
Between(Intersection.y, Line1.y1, Line1.y2) And _
Between(Intersection.y, Line2.y1, Line2.y2) Then
Intersection.Intersect = True
End If
Exit Function
Else
Slope1 = (Line1.y2 - Line1.y1) / (Line1.x2 - Line1.x1)
End If
If Line2Vertical Then
'Calculate X and Y as if they intersect
Slope1 = (Line1.y2 - Line1.y1) / (Line1.x2 - Line1.x1)
Intersection.x = Line2.x1
Intersection.y = Slope1 * Line2.x1 - (Slope1 * Line1.x1 - Line1.y1)
'does x,y lie on both lines?
If Between(Intersection.x, Line1.x1, Line1.x2) And _
Between(Intersection.x, Line2.x1, Line2.x2) And _
Between(Intersection.y, Line1.y1, Line1.y2) And _
Between(Intersection.y, Line2.y1, Line2.y2) Then
Intersection.Intersect = True
End If
Exit Function
Else
Slope2 = (Line2.y2 - Line2.y1) / (Line2.x2 - Line2.x1)
End If
If Slope1 = Slope2 Then Exit Function
'Get the Y intercepts
YInt1 = -(Slope1 * Line1.x1 - Line1.y1)
YInt2 = -(Slope2 * Line2.x1 - Line2.y1)
'-B = M * X -Y
'Check if the lines cross
If Slope1 = Slope2 And YInt1 <> YInt2 Then 'The lines are parallel
Exit Function
ElseIf Round(Slope1, 2) = Round(Slope2, 2) And YInt1 = YInt2 Then 'The lines are coincident
Exit Function
Else 'The lines cross somewhere
If Line1Horizontal Then
Intersection.y = Line1.y1
Intersection.x = (YInt2 - Line1.y1) / Slope2
ElseIf Line2Horizontal Then
Intersection.y = Line2.y1
Intersection.x = (YInt2 - Line2.y1) / Slope1
Else
Intersection.y = (YInt1 * (Slope2 / Slope1) - YInt2) / ((Slope2 / Slope1) - 1)
Intersection.x = (Intersection.y - YInt1) / Slope1
End If
End If
'does x,y lie on both lines?
If Between(Intersection.x, Line1.x1, Line1.x2) And _
Between(Intersection.x, Line2.x1, Line2.x2) And _
Between(Intersection.y, Line1.y1, Line1.y2) And _
Between(Intersection.y, Line2.y1, Line2.y2) Then
Intersection.Intersect = True
End If
End Function