CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Line segment intersection

    I need a C++ implementation of an efficient algorithm to detect line segment intersection. I'm reasonably familiar with Bentley-Ottmann, but it has limitations on what sorts of inputs it can handle that I'm pretty sure will be violated in my case. I've also read about this one:
    http://www.akira.ruc.dk/~keld/teachi...Chazelle92.pdf
    but unfortunately I don't have time to read through all that and it doesn't seem to include anything as useful as pseudocode or a step-by-step summary.

    I'm wondering if anyone knows a robust, public domain implementation out there which I can leverage until I have time to write my own version.

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Line segment intersection

    Hi,

    I don't know about a robust implementation, but the fastest hack-your-way-to-victory method I can think would be to express the line equations parametrically (x(t), y(t), z(t)), equate them, solve for t1 and t2 and then bounds check the parameters to verify that they are within the segment (instead of generically on the line).

    Example (in 2D, but I don't see why it wouldn't work in 3D):

    Do y=x and y=2x-1 intersect within the unit box?

    y=x parametrically:
    x(t1) = t1
    y(t1) = t1
    Where t1 in [0,1]

    y=2x-1 parametrically:
    x(t2)=2*t2-1
    y(t2) = t2
    Where t2 in [.5,1]

    Equate:
    x(t1)=y(t1): t1 = 2*t2-1
    y(t2)=y(t2): t1 = t2

    Solve (it's just a linear equation, use whatever method you like):
    t1 = 1; t2=1

    Query: t1 in [0,1]? Yes. t2 in [.5,1]? Yes. Both? Return true for intersection.

    Might need some special handling for equivalent lines (since they they have infinitely many solutions).
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Line segment intersection

    That works fine for two lines. I'm looking for the case when I have n lines, and I want to avoid O(n^2) time.

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Line segment intersection

    Ah, okay; I didn't appreciate that requirement. Bently-Ottmann algorithm looks interesting now that I look at it with it's O(n log n) complexity... wouldn't have thought you could beat naive in this case.
    Last edited by BioPhysEngr; February 8th, 2011 at 11:15 PM. Reason: misspelling
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

Featured