-
vb.net point of intersection
Hello. Im hoping someone here will be able to help me out. Iv been searching for a while now for a way to find the point of intersection between two drawn lines in vb.net and havn't found anything. The lines that are drawn will intersect at different points everytime they are generated and also they may be drawn at any angle. What im ultimately trying to achieve is a trim of the intersecting lines so they join at the point of intersection. Im quite new to vb.net so please forgive me if there is a simple answer to my question but any help is greatly appreciated.
-
Try this VB translation of this algorithm (found by searching for "geometry calculate lines intersection"):
Code:
Function LinesIntersect( _
ByVal Line1Start As Point, _
ByVal Line1End As Point, _
ByVal Line2Start As Point, _
ByVal Line2End As Point _
) As Point
Dim a1, a2, b1, b2, c1, c2, denom As Integer
Dim intersection As Point
a1 = Line1End.Y - Line1Start.Y
b1 = Line1Start.X - Line1End.X
c1 = Line1End.X * Line1Start.Y - _
Line1Start.X * Line1End.Y
a2 = Line2End.Y - Line2Start.Y
b2 = Line2Start.X - Line2End.X
c2 = Line2End.X * Line2Start.Y - _
Line2Start.X * Line2End.Y
denom = a1 * b2 - a2 * b1
If denom = 0 Then
' Lines do not intersect
Else
With intersection
.X = (b1 * c2 - b2 * c1) \ denom
.Y = (a2 * c1 - a1 * c2) \ denom
End With
End If
End Function
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Thanks a million .This is very helpful and very very much apreciated.I can't thank you enough.
-
ok.So that worked but now iv come to intersecting a line with a circle.Iv found some examples of how to find the intersecting points but i can't follow the math.Iv also found some that are written as functions in vb.net code and return an integer where as i need it to return a point(x,y).So can anyone help me with making a function that could do this or even help me to understand the math behind finding the point.Thanks in advance for any help.
-
Iv also just realised this function only works to an integer level.Even after modifying it to take singles and pointf's it still returns integers.So does anyone have a way to intersect 2 lines and return a point to the accuracy of atleast one decimal place?.Thanks in advance
-
to the decimal
ok,got it sorted.I found this page http://www.developerfusion.co.uk/show/1866/
which i modified only slightly to get this
Function LinesIntersect( _
ByVal Line1Start As PointF, _
ByVal Line1End As PointF, _
ByVal Line2Start As PointF, _
ByVal Line2End As PointF _
) As PointF
Dim Slope1 As Double
Dim Slope2 As Double
Dim Int1 As Double
Dim Int2 As Double
Dim intersection As PointF
'Get the slopes
If (Line1End.X - Line1Start.X) <> 0 Then
Slope1 = (Line1End.Y - Line1Start.Y) / (Line1End.X - Line1Start.X)
Else 'The slope is undefined
If (Line2End.X - Line2Start.X) = 0 Then
MsgBox("the lines do not cross")
Else
'Get Slope2 and YInt2
Slope2 = (Line2End.Y - Line2Start.Y) / (Line2End.X - Line2Start.X)
Int2 = (Slope2 * Line2Start.X - Line2Start.Y) * -1
'Claculate X and Y
intersection.X = Line1Start.X
intersection.Y = Slope2 * Line1Start.X - (Slope2 * Line2Start.X - Line2Start.Y)
End If
Return intersection
End If
If (Line2End.X - Line2Start.X) <> 0 Then
Slope2 = (Line2End.Y - Line2Start.Y) / (Line2End.X - Line2Start.X)
Else 'The slope is undefined
'Claculate X and Y
intersection.X = Line2Start.X
intersection.Y = Slope1 * Line2Start.X - (Slope1 * Line1Start.X - Line1Start.Y)
Return intersection
End If
'Get the Y intercepts
Int1 = -(Slope1 * Line1Start.X - Line1Start.Y)
Int2 = -(Slope2 * Line2Start.X - Line2Start.Y)
'Check if the lines cross
If Slope1 = Slope2 And Int1 <> Int2 Then 'The lines are parallel and don't cross
MsgBox("Error , lines do not intersect")
Else 'The lines cross somewhere
If Slope1 = 0 Then 'Line one is horizontal
intersection.Y = Line1Start.Y
intersection.X = (Int2 - Line1Start.Y) / Slope2
ElseIf Slope2 = 0 Then 'Line two is horizontal
intersection.Y = Line2Start.Y
intersection.X = (Int2 - Line2Start.Y) / Slope1
Else
intersection.Y = (Int1 * (Slope2 / Slope1) - Int2) / ((Slope2 / Slope1) - 1)
intersection.X = (intersection.Y - Int1) / Slope1
End If
End If
Return intersection
End Function
this seems to work good.Hopefully it will help someone else along the way aswell.
I take no credit at all for this code.All credit goes to Nick Avery,i just modified his work to suit my needs.Thanks Nick.
Similar Threads
-
Replies: 36
Last Post: 10-22-2002, 03:11 PM
-
By Steven Bell in forum .NET
Replies: 260
Last Post: 06-01-2001, 04:32 PM
-
By Tom Cabanski in forum .NET
Replies: 74
Last Post: 03-14-2001, 03:30 PM
-
By Mark Burns in forum .NET
Replies: 164
Last Post: 03-13-2001, 12:43 PM
-
By David Kroll in forum .NET
Replies: 33
Last Post: 02-13-2001, 10:23 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|