# Distance between two line segments defined by sparse points

Given a set S of line segments and an additional segment L, I'm looking for a procedure that determines which of the line segments in S is nearest to L. My first thought was to compute the distance between the points of L and the points of each line segment in S, however it can be tricky as illustrated in the following figure: Let's say that L is the blue line, I want my method to point out that the green one is the nearest, but computing the distance between the points in such example may not provide the intended result.

Therefore my question is: which is the simplest (and correct) way to address this problem.

Thank you so much for your help.

edit: note that the lines are not always parallel, horizontal nor straight

• To make the question complete, are the line segments always horizontal? Parallel? Nov 13, 2018 at 18:47
• unfortunately not always Nov 13, 2018 at 18:48
• they can be curve as well Nov 13, 2018 at 18:49
• I have an approach that could work but it is not detailed to the equation level. Its a general algorithm I came up with. I am not sure it would work in all cases since I just made it up. If I publish it here the question will show as if it has an answer and this may prevent others from looking at it. If you don't get replies and want to see my idea let me know. My idea is not trivial to implement as a program but it is not too difficult either. Nov 13, 2018 at 20:47
• Thank you for your effort. I think you should post it as an answer as it may help others in the future. As for not working in all cases or any sort of limitations in your algorithm, you can point them out in your answer, e.g. specify the scenarios in which your algorithm works well and which ones it does not. This way the readers can determine if it is an appropriate solution for their problem at hand Nov 14, 2018 at 8:59 