
Mar 12th, 2013, 05:17 PM
#1
intersection point slightly off
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 thanks
Code:
Public Class Form1
'getnode code from:
'http://social.msdn.microsoft.com/Forums/enUS/vbgeneral/thread/4eb3423eeb8149778ce55a568d53fd9b/
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
 Coding Examples:
 Features:
 Online Games:
 Compiled Games:

Mar 13th, 2013, 12:00 AM
#2
Re: intersection point slightly off
It looks like rounding error is throwing the result off. At some point there's nothing that can be done, but at least the ycoordinate can be made a bit more accurate by not rounding x to an integer before computing y. Try...
Code:
...
Dim xd As Double= ((C.Y  A.Y) * dx1 * dx2 + dy1 * dx2 * A.X  dy2 * dx1 * C.X) / (dy1 * dx2  dy2 * dx1)
Dim x As Integer = CInt(xd)
Dim y As Integer = CInt(A.Y + (dy1 / dx1) * (xd  A.X))
...
The time you enjoy wasting is not wasted time.
Bertrand Russell
< Remember to rate posts you find helpful.

Mar 13th, 2013, 10:49 AM
#3
Re: intersection point slightly off
Yeah... what Jemidiah said.
But to go further, why are you mucking around with Integers at all? In your comments you linked to a forum post which gave you your getnode() function, and the only thing you changed was the types of the variables x and y from Double to Integer. Given that you are dealing with a Cartesian plane where almost all points are nonintegers, introducing Integer datatypes is a recipe for rounding error. If at some point you need to map values to a integerbased screen coordinate system, you can do so at the presentation layer. As a general rule, though, you should avoid mixing integer and noninterger datatypes as much as possible.... they just don't mix very well.
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

Click Here to Expand Forum to Full Width
Survey posted by VBForums.
