2

I have a set of lines L and a set of points P. I want to find how many lines of L intersect by a horizontal line passing through a point p. How can i compute this?

5
  • Are you expecting to do this without looking at every line in L (say, by using some optimises container datatype)? Commented Feb 3, 2021 at 22:20
  • 1
    Do you have a set of lines or a set of line segments? In the former case it's simply checking which lines are parallel to the X axis. Commented Feb 4, 2021 at 0:04
  • 1
    Also how are your lines L stored, assuming they're line segments? As pairs of points (start/end)? Commented Feb 4, 2021 at 0:06
  • What if a line segment is intersected by multiple horizontal lines originating from points in P? Does that count multiple times or only once? Commented Feb 4, 2021 at 0:10
  • Lines or line segments ?? How many lines ? How many points ? Commented Feb 4, 2021 at 9:04

1 Answer 1

1

Assuming your set of line segments are stored as (start, stop) pairs and multiple intersections count multiple times, this answer applies.

The first step is to throw away all x coordinates - only the y coordinates matter. Then construct an array of pairs from L and P. For each line segment in L add (y_start, START) and (y_stop, STOP) to the array. For each point in P add (y, POINT) to the array (START, STOP, POINT are just arbitrary values, e.g. an C enum). Sort the array of pairs by the first value.

Then, initialize n = 0, l = 0 and loop through the array and look at the second value of each pair:

  1. If it is START, increment l.
  2. If it is STOP, decrement l.
  3. If it is POINT, add l to n.

n is your final result. Total complexity is dominated by the sort, O((|L| + |P|) log(|L| + |P|)).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.