1
$\begingroup$

Assuming we have a fixed set of line segments $S$ such that any two segments are either disjoint or have a common endpoint.

A query would look like this; given a query point $q$ shoot 4 rays from $q$ (both sides horizontally and both sides vertically) and report the line segment in $S$ closest to $q$ intersecting any on these rays.

Is there a way to do this faster than $O(n)$ by preprocessing the data and storing it in some data structure? Or do we have to check for intersections with all segments for each ray and then compute the distances from the intersected line segments to $q$?

asked Mar 21, 2021 at 12:26
$\endgroup$
1

1 Answer 1

2
$\begingroup$

You'll need two data structures - for vertical rays and for horizontal rays. I'll describe a data structure for vertical rays. Draw a vertical line through each segment end - you'll get a set of vertical slabs. Each of these slabs will be divided by pieces of original segments into trapezoids (possibly infinite ones).

SlabsForThreeSegments

Number of these slabs will not exceed 2ドルn$, and number of trapezoids in each slab will not exceed $n$. In order to find a segment, closest to the query point along a vertical ray (going up or down), you need to use binary search to find a slab at first, and then - another binary search to find a trapezoid inside the found slab. Upper and lower boundary of this trapezoid will give you the answer. These two binary searches will take $O(log(n))$ time.

The data structure for horizontal rays is similar - the only difference is that slabs should be horizontal.

answered Mar 22, 2021 at 3:50
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.