the code i've posted is supposed to find the intersection point of 2 lines, but it's slightly wrong. i make the point of intersection 122,42, but i think getnode (i found this function on msdn) finds 122,40...
i was trying to solve the problem myself but i was confused with the slope factor of the 2 lines.
an explanation of how to calculate this taking in to consideration the slope factor(s) of the lines would be appreciated, + a fix for the existing code would also be appreciated:
as usual please keep any answers as simple as possible:D thanks:D
i was trying to solve the problem myself but i was confused with the slope factor of the 2 lines.
an explanation of how to calculate this taking in to consideration the slope factor(s) of the lines would be appreciated, + a fix for the existing code would also be appreciated:
as usual please keep any answers as simple as possible:D thanks:D
Code:
Public Class Form1
'getnode code from:
'http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/4eb3423e-eb81-4977-8ce5-5a568d53fd9b/
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawLine(Pens.Black, 120, 20, 130, 120)
e.Graphics.DrawLine(Pens.Black, 50, 50, 150, 40)
'122,42
Dim intersectionPoint As Point = getnode(New Point(120, 20), New Point(130, 120), New Point(50, 50), New Point(150, 40))
intersectionPoint.Offset(-5, -2)
e.Graphics.FillEllipse(Brushes.Red, New Rectangle(intersectionPoint, New Size(10, 10)))
End Sub
Public Function getnode(ByVal A As Point, ByVal B As Point, ByVal C As Point, ByVal D As Point) As Point
Dim dy1 As Double = B.Y - A.Y
Dim dx1 As Double = B.X - A.X
Dim dy2 As Double = D.Y - C.Y
Dim dx2 As Double = D.X - C.X
Dim p As New Point
'check whether the two line parallel
If dy1 * dx2 = dy2 * dx1 Then
MessageBox.Show("no point")
'Return P with a specific data
Else
Dim x As Integer = CInt(((C.Y - A.Y) * dx1 * dx2 + dy1 * dx2 * A.X - dy2 * dx1 * C.X) / (dy1 * dx2 - dy2 * dx1))
Dim y As Integer = CInt(A.Y + (dy1 / dx1) * (x - A.X))
p = New Point(x, y)
Return p
End If
End Function
End Class