Just a few points of clarification:
- do the "closest" lines have to be distance = 0?
- does every line segment need to be merged with one other line segment, or will some remain un-merged?
- once a line segment is merged, does it become a candidate for other line segments to merge onto?
- can you describe the steps (i.e. the algorithm) that will produce the results you're after?
- your example of three segments can be solved very easily, but what about if you had 4 segments (10,5,5,15)? I guess if you start at the beginning you would be left with two segments (15,20). But, what if the first segment you considered happened to be in the second position (5). Then, you'd be left with three segments (10,10,15). Basically, it will depend greatly on the order in which you consider the segments.