alien Posted October 16, 2007 Posted October 16, 2007 (edited) Hi I need help on how i can find the angles and point of intersection between two lines that i have drawn on the form. I am using VB.net 2005 Edited October 16, 2007 by alien Quote
Administrators PlausiblyDamp Posted October 20, 2007 Administrators Posted October 20, 2007 Dug up a routine I originally hacked from some C++ code i found on the internet (can't remember where though) This should find the intersection point of two lines (hasn't been tested overly though but it seemed to work at the time from what I remember) 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 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
alien Posted November 23, 2007 Author Posted November 23, 2007 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 cheers Quote
Leaders snarfblam Posted November 25, 2007 Leaders Posted November 25, 2007 If you want to find the angle of the intersection then you need to study up on some trigenometry. I'll 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.