We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 16,877 times

Related Categories

Intersecting Lines

liserdarts

This code finds where two lines intersect just by giving this function two X and Y coordinates on the two lines.
<i>If you plan to use this code in some application, take a look at how the code handles two lines that don't cross(parallel), and when you give it the same line. Also the code can't handle vertical lines.</i>



'*****
'Nick Avery
'liserdarts@yahoo.com
'Updated 14, April, 2002
'*****
Private Type TDLine
  X As Double
  Y As Double
  X2 As Double
  Y2 As Double
End Type

Private Type TDPoint
  X As Double
  Y As Double
End Type

Private Function LinesCross(Line1 As TDLine, Line2 As TDLine) As TDPoint
Dim Slope1 As Double
Dim Slope2 As Double
Dim YInt1 As Double
Dim YInt2 As Double

   'Get the slopes
       If (Line1.X2 - Line1.X) <> 0 Then
           Slope1 = (Line1.Y2 - Line1.Y) / (Line1.X2 - Line1.X)
       Else 'The slope is undefined
           If (Line2.X2 - Line2.X) = 0 Then 'Both the lines are vertical and don't cross
               LinesCross.X = 0
               LinesCross.Y = 0
           Else
               'Get Slope2 and YInt2
                   Slope2 = (Line2.Y2 - Line2.Y) / (Line2.X2 - Line2.X)
                   YInt2 = (Slope2 * Line2.X - Line2.Y) * -1
               'Claculate X and Y
                   LinesCross.X = Line1.X
                   LinesCross.Y = Slope2 * Line1.X - (Slope2 * Line2.X - Line2.Y)
           End If
           Exit Function
       End If
       If (Line2.X2 - Line2.X) <> 0 Then
           Slope2 = (Line2.Y2 - Line2.Y) / (Line2.X2 - Line2.X)
       Else 'The slope is undefined
           'Claculate X and Y
               LinesCross.X = Line2.X
               LinesCross.Y = Slope1 * Line2.X - (Slope1 * Line1.X - Line1.Y)
           Exit Function
       End If
       
   'Get the Y intercepts
       YInt1 = -(Slope1 * Line1.X - Line1.Y)
       YInt2 = -(Slope2 * Line2.X - Line2.Y)
       
       '-B = M * X -Y
               
   'Check if the lines cross
       If Slope1 = Slope2 And YInt1 <> YInt2 Then 'The lines are parallel and don't cross
           LinesCross.X = 0
           LinesCross.Y = 0
       ElseIf Round(Slope1, 2) = Round(Slope2, 2) And YInt1 = YInt2 Then 'It is the same line and they cross at every point on that line
           LinesCross.X = Line1.X
           LinesCross.Y = Line1.Y
       Else 'The lines cross some ware
           If Slope1 = 0 Then 'Line one is horizontal
               LinesCross.Y = Line1.Y
               LinesCross.X = (YInt2 - Line1.Y) / Slope2
           ElseIf Slope2 = 0 Then 'Line two is horizontal
               LinesCross.Y = Line2.Y
               LinesCross.X = (YInt2 - Line2.Y) / Slope1
           Else
               LinesCross.Y = (YInt1 * (Slope2 / Slope1) - YInt2) / ((Slope2 / Slope1) - 1)
               LinesCross.X = (LinesCross.Y - YInt1) / Slope1
           End If
       End If
End Function

Private Sub Form_Load()
Dim Line1 As TDLine
Dim Line2 As TDLine
Dim Point As TDPoint
   
   Me.AutoRedraw = True
   Me.Scale (-50, 50)-(50, -50)
   
   Line1.X = -90
   Line1.Y = 25
   Line1.X2 = 35
   Line1.Y2 = 15
   
   Line2.X = 25
   Line2.Y = 5
   Line2.X2 = -16
   Line2.Y2 = 45
   
   Point = LinesCross(Line1, Line2)
       
   
   Me.Line (Line1.X, Line1.Y)-(Line1.X2, Line1.Y2)
   Me.Line (Line2.X, Line2.Y)-(Line2.X2, Line2.Y2)

   Me.Circle (Point.X, Point.Y), 5
End Sub
/html>

I am as a web developer for a small company, working for a small company. I work on banking websites and verious related projects.

by: Nick Avery liserdarts@yahoo.com

Comments

  • Re: Do segments themselves actually cross

    Posted by Pompoei on 12 Nov 2007

    I have transformed your code for my purposes using Borland c++ Builder 6. Thanx for your unknowing help. You saved me ALOT of trouble.

    bool CalcIntersect(double xc1,double yc1,double xc2,double ...

  • Do segments themselves actually cross

    Posted by Dayrelton on 31 May 2003

    Nick.

    I took your code and added the following.
    1) Return boolean as to whether or not the line segments actually cross
    2) If lines are parallel or coincident return false, thus x,y can be ignore...