Dug up a routine I originally hacked from some C++ code i found on the internet (cant remember where though)
This should find the intersection point of two lines (hasnt been tested overly though but it seemed to work at the time from what I remember)
Code:
Public Class Line
Public Sub New(ByVal startPoint As PointF, ByVal endPoint As PointF)
_StartPoint = startPoint
_EndPoint = endPoint
End Sub
Public ReadOnly Property StartPoint() As PointF
Get
Return _StartPoint
End Get
End Property
Public ReadOnly Property EndPoint() As PointF
Get
Return _EndPoint
End Get
End Property
Private ReadOnly _StartPoint, _EndPoint As PointF
Enum IntersectResult
Parallel
Coincident
DoNotIntersect
Intersect
End Enum
Public Shared Function Intersect(ByVal lineOne As Line, ByVal lineTwo As Line, ByRef intersection As PointF) As IntersectResult
Dim denominator As Double = ((lineOne._EndPoint.Y - lineOne._StartPoint.Y) * (lineTwo._EndPoint.X - lineTwo._StartPoint.X)) - _
((lineOne._EndPoint.X - lineOne._StartPoint.X) * (lineTwo._EndPoint.Y - lineTwo._StartPoint.Y))
Dim numeratorOne As Double = ((lineOne._EndPoint.X - lineOne._StartPoint.X) * (lineTwo._StartPoint.Y - lineOne._StartPoint.Y)) - _
((lineOne._EndPoint.Y - lineOne._StartPoint.Y) * (lineTwo._StartPoint.X - lineOne._StartPoint.X))
Dim numeratorTwo As Double = ((lineTwo._EndPoint.X - lineTwo._StartPoint.X) * (lineTwo._StartPoint.Y - lineOne._StartPoint.Y)) - _
((lineTwo._EndPoint.Y - lineTwo._StartPoint.Y) * (lineTwo._StartPoint.X - lineOne._StartPoint.X))
If denominator = 0 Then
If numeratorOne = 0.0 AndAlso numeratorTwo = 0.0 Then
Return IntersectResult.Coincident
End If
Return IntersectResult.Parallel
End If
Dim a As Double = numeratorOne / denominator
Dim b As Double = numeratorTwo / denominator
If a >= 0.0 AndAlso a <= 1.0 AndAlso b >= 0.0 AndAlso b <= 1.0 Then
intersection.X = lineTwo._StartPoint.X + a * (lineTwo._EndPoint.X - lineTwo._StartPoint.X)
intersection.Y = lineTwo._StartPoint.Y + a * (lineTwo._EndPoint.Y - lineTwo._StartPoint.Y)
Return IntersectResult.Intersect
End If
Return IntersectResult.DoNotIntersect
End Function
End Class
You can call it like
Code:
Dim l1 As New Line(New Point(0, 0), New Point(100, 100))
Dim l2 As New Line(New Point(100, 0), New Point(0, 100))
Dim p As PointF
Dim i As Line.IntersectResult = Line.Intersect(l1, l2, p)
Where after the function returns p will be the point of intersection - you will need to check the return value though to make sure the lines do actually intersect.
Hi thanks alot for the code. although Iam still having trouble finding the angle of intersection between two lines. what Iam trying to achieve is to get the reflection of a line once it intersects with another line. Kinda like a mirror image of the line. Please help. I am using vb.net 2005
If you want to find the angle of the intersection then you need to study up on some trigenometry. Ill be more than glad to help if you get stuck, but you really need to find a trig website, which will explain how to get the angle of a line based on its slope. The angle of the intersection will be the difference of the angle of the lines, and the reflected angle will simply be the compliment to the angle of intersection.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.