I can find plenty formulas for finding the distance between two skew lines. I want to calculate the distance between two line segments in one dimension.
It's easy to do with a bunch of IF statements. But I was wondering if their is a more efficient math formula.
E.g. 1:
----L1x1-------L2x1-------L1x2------L2x2----------------------------
L1 = line segment 1, L2 = line segment 2; the distance here is 0 because of intersection
E.g. 2:
----L1x1-------L1x2-------L2x1------L2x2----------------------------
the distance here is L2x1 - L1x2
EDIT:
The only assumption is that the line segments are ordered, i.e. x2 is always > x1.
Line segment 1 may be to the left, right, equal to etc. of line segment 2. The algorithm has to solve for this.
EDIT 2:
I have to implement this in T-SQL (SQL Server 2008). I just need the logic... I can write the T-SQL.
EDIT 3:
If a line segment is a line segment of the other line, the distance is 0.
----L1x1-------L2x1-------L2x2------L1x2----------------------------
Line segment 2 is a segment of line segment 1, making the distance 0.
If they intersect or touch, the distance is 0.
x2 >= x1
(not sure if this is the case)? – Ani