Results 1 to 6 of 6

Thread: Intersecting Line Segments

  1. #1

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Resolved Intersecting Line Segments

    I'm down to the final step of a progie of mine, just have to build an intersection test.


    Whats the best way to determine if 2 line segments intersect.

    I figure, given 2 line segments, 1-2, 3-4:

    1. Test for parallelity, return False if parallel.
    2. If Not parallel, calc intersection points of the 2 lines. (Call it 5)
    3. Return False if one or more of the following distance comparisons are true:

      • 5 to 1 > 1 to 2
      • 5 to 2 > 1 to 2
      • 5 to 3 > 3 to 4
      • 5 to 4 > 3 to 4
    4. Else return True.


    Is there a more optimal strategy?

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Intersecting Line Segments

    Sure, use counter-clockwise testing...

    This algorithm has the 2 functions you require...

    http://support.microsoft.com/default...;EN-US;q121960

    You'll have to translate it into your language of choice, its not difficult though. You need the "CCW" (CounterClockWise) and "Intersect" functions. The code is tiny and very fast.

    Its a brilliant article actually. Well worth reading the whole algorithm, I learned a lot from it.

    NOTE:
    You'll have to use IE to view that page because it doesn't come out right in FireFox, that's MS for ya!
    Last edited by wossname; Mar 17th, 2005 at 03:53 PM.
    I don't live here any more.

  3. #3

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: Intersecting Line Segments

    Thanks Woss!

    I added a little extra, because in my progie, if an endpoint is shared between two line segments, then its considered non-intersecting.

    s0, this is what it turned out to be:

    VB Code:
    1. Public Function ARE_PTS_EQUAL(ByRef mPt1 As pt, ByRef mPt2 As pt) As Boolean
    2.     If (mPt1.mX = mPt2.mX) And (mPt1.mY = mPt2.mY) Then
    3.         ARE_PTS_EQUAL = True
    4.     Else
    5.         ARE_PTS_EQUAL = False
    6.     End If
    7. End Function
    8. Public Function MS_INTERSECT(ByRef mPt1 As pt, ByRef mPt2 As pt, ByRef mPt3 As pt, ByRef mPt4 As pt) As Boolean
    9.     If ARE_PTS_EQUAL(mPt1, mPt3) Or ARE_PTS_EQUAL(mPt1, mPt4) Or ARE_PTS_EQUAL(mPt2, mPt3) Or ARE_PTS_EQUAL(mPt2, mPt4) Then
    10.         MS_INTERSECT = False
    11.     Else
    12.         MS_INTERSECT = ((CCW(mPt1, mPt2, mPt3) * CCW(mPt1, mPt2, mPt4)) <= 0) And ((CCW(mPt3, mPt4, mPt1) * CCW(mPt3, mPt4, mPt2)) <= 0)
    13.     End If
    14. End Function
    15. Public Function CCW(ByRef mPt0 As pt, ByRef mPt1 As pt, ByRef mPt2 As pt) As Integer
    16.     Dim dX1 As Long
    17.     Dim dX2 As Long
    18.     Dim dY1 As Long
    19.     Dim dY2 As Long
    20.  
    21.     dX1 = mPt1.mX - mPt0.mX
    22.     dX2 = mPt2.mX - mPt0.mX
    23.     dY1 = mPt1.mY - mPt0.mY
    24.     dY2 = mPt2.mY - mPt0.mY
    25.  
    26.     If (dX1 * dY2 > dY1 * dX2) Then
    27.       CCW = 1
    28.     Else
    29.       CCW = -1
    30.     End If
    31. End Function

    Much nicer than what I had designed.
    I actually have something similar to CCW:

    VB Code:
    1. Public Function SIGN_OF_SIDE(ByRef tPt As pt, ByRef mPt1 As pt, ByRef mPt2 As pt) As Integer
    2.     Dim MyOut As Double
    3.     MyOut = (tPt.mY - mPt1.mY) * (mPt2.mX - mPt1.mX) - (tPt.mX - mPt1.mX) * (mPt2.mY - mPt1.mY)
    4.     SIGN_OF_SIDE = Sgn(MyOut)
    5. End Function

    and using that instead of CCW returns identical results for the intersection test.

    So, Now I've got a Mesh Generator!
    I'm trying to eventually make a contour mapper.


  4. #4
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537

    Re: Intersecting Line Segments

    THANK YOU NotLKH YOUR POST TOTALLY SOLVED MY PROBLEM!!!!
    In life you can be sure of only two things... death and taxes. I'll take death.

  5. #5

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: Intersecting Line Segments

    Your Quite Welcome!


    cough, reputation, cough
    Attached Images Attached Images  

  6. #6
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537

    Re: Intersecting Line Segments

    I had converted it to VB.NET but not realized that shared points were being considered.

    Thanks again.
    In life you can be sure of only two things... death and taxes. I'll take death.

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