intersection point slightly off-VBForums
Results 1 to 3 of 3

Thread: intersection point slightly off

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Chelmsford UK

    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

    Public Class Form1
        'getnode code from:
        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)
            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
                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

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002

    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 y-coordinate can be made a bit more accurate by not rounding x to an integer before computing y. Try...

                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.

  3. #3
    Hyperactive Member Lenggries's Avatar
    Join Date
    Sep 2009

    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 non-integers, introducing Integer datatypes is a recipe for rounding error. If at some point you need to map values to a integer-based screen coordinate system, you can do so at the presentation layer. As a general rule, though, you should avoid mixing integer and non-interger 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


Click Here to Expand Forum to Full Width