Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 236685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:21:53+00:00 2026-05-11T20:21:53+00:00

I have a ray, I need to find the closest line segment that it

  • 0

I have a ray, I need to find the closest line segment that it hits. I think it’s possible to do this in O(log n) time if I sort the line segments first, but I can’t remember how to sort them… I think some sort of tree would work best, but how do I sort them by both start and end point? I would also like fast insertions into this data structure if possible.

There’s lots of code for one ray vs one line segment, but I need something for one ray vs many line segments… I don’t know what terms to google for.

A link to an appropriate article is good, C++ code is even better. Thanks! 🙂

PS: The line segments are actually the edges of a non-self-intersecting polygon, sorted in CCW order… but I think there may be some advantage to sorting them in a different fashion?

This is all 2D.


On second thought, I’m not entirely sure this is possible. Some sort of spatial partitioning might help, but otherwise, I can’t think of any way to sort the lines so that they could be compared with an arbitrary ray.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:21:54+00:00Added an answer on May 11, 2026 at 8:21 pm

    You could take a bounding box of the polygon (min-max x,y coordinates) and build a grid inside the box. Then, for each cell, remember all lines that cross the cell.

    Find an intesection like this:

    • Find out which cell the ray hits first (O(1))
    • Use Grid traversal algorithm to “draw” a ray through the grid. When you hit non-empty cell, check all its lines, check if intersection is inside the cell and pick the closest intersection. If all intersections are outside the cell, continue (this is O(grid length)).

    You could also make the grid hierarchical (ie. quadtree – a tree you were asking for), and walk it using the same algorithm. This is done in raytracing in 3D and the time complexity is O(sqrt(N)).


    Or, use the approach I did in my raytracer:

    • Build a quadtree containing the lines (building quadtree is desribed in the article) – you split nodes (=areas) if they contain too many objects into 4 sub-nodes (sub-areas)
    • Collect all leaf nodes of the quadtree that are hit by the ray:

      Compute ray-rectangle intersection (not hard) for the root. If the root is hit by the ray, proceed with its children recursively.

    The cool thing about this is that when a tree node is not hit, you’ve skipped processing whole subtree (potentially a large rectangular area).

    In the end, this is equivalent to traversing the grid – you collect the smallest cells on the ray’s path, and then test all objects in them for intersection. You just have to test all of them and pick the closest intersection (so you explore all lines on ray’s path).

    This is O(sqrt(N)).

    In grid traversal, when you find an intersection, you can stop searching. To achieve this with quadtree traversal, you would have to seach the children in the right order – either sort the 4 rect intersections by distance or cleverly traverse the 4-cell grid (an we are back to traversal).

    This is just a different approach, comparatively same difficult to implement I think, and works well (I tested it on real data – O(sqrt(N))). Again, you would only benefit from this approach if you have at least a couple of lines, when the polygon has 10 edges the benefit compared to just testing all of them would be little I think.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you managed to get Aptana Studio debugging to work? I tried following this,
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have you determined a maximum number of characters allowed in FCKEditor ? I seem
Have a n-tire web application and search often times out after 30 secs. How
Have had to write my first proper multithreaded coded recently, and realised just how
Have you refactored from an ActiveRecord to a DataMapper pattern? What conditions prompted the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.