Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[1866] Intersecting Lines

Last post 11-12-2007 10:50 AM by Pompoei. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [1866] Intersecting Lines

    This thread is for discussions of Intersecting Lines.

    • Post Points: 0
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 05-31-2003 11:18 AM In reply to

    Do segments themselves actually cross

    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 ignored
    3) Otherwise x,y are the point of intersection (where they do or would intersect)

    code kept simple to be less confusing, can be collapsed to be more efficient less readable.

    Public Function Intersection(Line1 As LineSegment, Line2 As LineSegment) As Intersect
       'if Line1 and Line2 intersect, return Intersect as True and the X,Y point of intersection
       'otherwise, they are parallel, coincident or do not intesect, return Intersect as False
       Dim Slope1 As Double
       Dim Slope2 As Double
       Dim YInt1 As Double
       Dim YInt2 As Double
       Dim Line1Vertical As Boolean
       Dim Line2Vertical As Boolean
       Dim Line1Horizontal As Boolean
       Dim Line2Horizontal As Boolean

       'rule out simple cases
       If Line1.x1 = Line1.x2 Then Line1Vertical = True
       If Line2.x1 = Line2.x2 Then Line2Vertical = True
       If Line1.y1 = Line1.y2 Then Line1Horizontal = True
       If Line2.y1 = Line2.y2 Then Line2Horizontal = True
       If Line1Vertical And Line2Vertical Or Line1Horizontal And Line2Horizontal Then Exit Function 'false
           
       'Get the slopes, handle simple case of eiher line vertical - both cannot due to above cases
           If Line1Vertical Then
               'Calculate X and Y as if they intersect
               Slope2 = (Line2.y2 - Line2.y1) / (Line2.x2 - Line2.x1)
               Intersection.x = Line1.x1
               Intersection.y = Slope2 * Line1.x1 - (Slope2 * Line2.x1 - Line2.y1)
               'does x,y lie on both lines?
               If Between(Intersection.x, Line1.x1, Line1.x2) And _
                  Between(Intersection.x, Line2.x1, Line2.x2) And _
                  Between(Intersection.y, Line1.y1, Line1.y2) And _
                  Between(Intersection.y, Line2.y1, Line2.y2) Then
                   Intersection.Intersect = True
                 End If
               Exit Function
             Else
               Slope1 = (Line1.y2 - Line1.y1) / (Line1.x2 - Line1.x1)
             End If
           If Line2Vertical Then
               'Calculate X and Y as if they intersect
               Slope1 = (Line1.y2 - Line1.y1) / (Line1.x2 - Line1.x1)
               Intersection.x = Line2.x1
               Intersection.y = Slope1 * Line2.x1 - (Slope1 * Line1.x1 - Line1.y1)
               'does x,y lie on both lines?
               If Between(Intersection.x, Line1.x1, Line1.x2) And _
                  Between(Intersection.x, Line2.x1, Line2.x2) And _
                  Between(Intersection.y, Line1.y1, Line1.y2) And _
                  Between(Intersection.y, Line2.y1, Line2.y2) Then
                   Intersection.Intersect = True
                 End If
               Exit Function
             Else
               Slope2 = (Line2.y2 - Line2.y1) / (Line2.x2 - Line2.x1)
             End If
           
           If Slope1 = Slope2 Then Exit Function
           
           'Get the Y intercepts
           YInt1 = -(Slope1 * Line1.x1 - Line1.y1)
           YInt2 = -(Slope2 * Line2.x1 - Line2.y1)
           
           '-B = M * X -Y
                   
           'Check if the lines cross
           If Slope1 = Slope2 And YInt1 <> YInt2 Then 'The lines are parallel
               Exit Function
             ElseIf Round(Slope1, 2) = Round(Slope2, 2) And YInt1 = YInt2 Then 'The lines are coincident
               Exit Function
             Else 'The lines cross somewhere
               If Line1Horizontal Then
                   Intersection.y = Line1.y1
                   Intersection.x = (YInt2 - Line1.y1) / Slope2
                 ElseIf Line2Horizontal Then
                   Intersection.y = Line2.y1
                   Intersection.x = (YInt2 - Line2.y1) / Slope1
                 Else
                   Intersection.y = (YInt1 * (Slope2 / Slope1) - YInt2) / ((Slope2 / Slope1) - 1)
                   Intersection.x = (Intersection.y - YInt1) / Slope1
                 End If
             End If
           'does x,y lie on both lines?
           If Between(Intersection.x, Line1.x1, Line1.x2) And _
              Between(Intersection.x, Line2.x1, Line2.x2) And _
              Between(Intersection.y, Line1.y1, Line1.y2) And _
              Between(Intersection.y, Line2.y1, Line2.y2) Then
               Intersection.Intersect = True
             End If
     End Function
    • Post Points: 10
  • 11-12-2007 10:50 AM In reply to

    • Pompoei
    • Not Ranked
    • Joined on 11-12-2007
    • South Africa
    • New Member
    • Points 5

    Re: Do segments themselves actually cross

    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 yc2, double xc3,double yc3,double xc4,double yc4, double &icx,double &icy) { //x and y are used for line 1, u and v for line 2 //input x1,y1 input x2,y2 input u1,v1 input u2,v2 double x1,y1,x2,y2 = 0; double u1,v1,u2,v2 = 0; double b1,b2,a1,a2,xi,yi = 0; x1 = xc1; y1 = yc1; x2 = xc2; y2 = yc2; u1 = xc3; v1 = yc3; u2 = xc4; v2 = yc4; //rule out simple cases bool Line1Vertical = false; bool Line2Vertical = false; bool Line1Horizontal = false; bool Line2Horizontal = false; //If both lines are vertical or horizontal if(x1 == x2){Line1Vertical = True;} if(u1 == u2){Line2Vertical = True;} if(y1 == y2){Line1Horizontal = True;} if(v1 == v2){Line2Horizontal = True;} if(Line1Vertical == true && Line2Vertical == true || Line1Horizontal == true && Line2Horizontal == true) { return(false); } if(Line1Vertical == true) { //Calculate X and Y as if they intersect b2 = (v2-v1)/(u2-u1); icx = x1; icy = b2 * u1 - (b2 * u1 - v1); //does x,y lie on both lines? if(icx >= x1 && icx = u1 && icx = y1 && icx = v1 && icx = x1 && icx = u1 && icx = y1 && icx = v1 && icx = x1 && icx = u1 && icx = y1 && icx = v1 && icx

    Jesus did it. John 3:16. May you be Blessed as I am.
    • Post Points: 5
Page 1 of 1 (3 items)