Efficient algorithm for shortest distance between two line segments in 1D?

" The answer depends slightly on whether you already know which range is smallest already, and whether the points in the ranges are ordered correctly (that is, whether the lines have the same direction) if (a. Start Start) { first = a; second = b; } else { first = b; second = a; } Then: distance = max(0, second. Start - first.

End) Depending on where you're running this, your compiler should optimise it nicely. In any case, you should probably profile to make sure that your code is a bottleneck before making it less readable for a theoretical performance improvement.

" The answer depends slightly on whether you already know which range is smallest already, and whether the points in the ranges are ordered correctly (that is, whether the lines have the same direction). If (a. Start End); Depending on where you're running this, your compiler should optimise it nicely.

In any case, you should probably profile to make sure that your code is a bottleneck before making it less readable for a theoretical performance improvement.

Brilliant! Took – IanC Dec 15 '10 at 11:48.

This works in all cases: d = (s1 max s2 - e1 min e2) max 0 As a bonus, removing max 0 means a negative result indicates exactly how much of the two segments overlap. Proof Note that the algorithm is symmetric, so asymmetric cases only need to covered once. So I'm going to assert s2 >= s1 w.l.o.g.

Also note e1 >= s1 and e2 >= s2. Cases: L2 starts after L1 ends (s2 >= e1): s1 max s2 = s2, e1 min e2 = e1. Result is s2 - e1, which is non-negative and clearly the value we want (the distance).

L2 inside L1 (s2 = e1): s1 max s2 = s2, e1 min e2 = e1. S2 - e1 is non-positive by s2.

1 +1. Or more in the posters syntax d = max(0, max(L1x1,L2x1)-min(L1x2,L2x2) ) – phkahler Dec 15 '10 at 12:09 This is what I had written: MAX(0,IF(L1x1It isn't pretty there at all :) – IanC Dec 15 '10 at 12:55.

I do not think there is a way around the conditions. But this is succinct: var diff1 = L2x1 - L1x2; var diff2 = L2x2 - L1x1; return diff1 > 0? Max(0, diff1) : -min(0,diff2); This assumes LNx1.

The lines may overlap and may be in any relationship to each other (left, right, in, intersecting, etc. ). – IanC Dec 15 '10 at 11:38 It handles them all. Just test it in the code.

– Aliostad Dec 15 '10 at 11:39.

I think since all line segments in the 1D is one of form (X,0) or (0,Y) so store all these x values in a array and sort the array and minimum distance will the differece between 1st 2 elemenst of the array. Here you need to be careful while storing element in the array so that duplicate elemenst are not stored.

That won't return a 0 where Line 1 is a segment of Line 2. – IanC Dec 15 '10 at 11:39.

This formula seems to work in all cases but the one where one line lies fully on the other line. Return -min(a2-b1,b2-a1).

I tried this. If Line 1 is a segment of Line 2, the result should be 0. – IanC Dec 15 '10 at 11:37 Ah, I was just editing it out to add that note :) – Max Dec 15 '10 at 11:38.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions