title: Need a programming algorithm for a tight enclosure around line segments
body: <p><a href="https://i.stack.imgur.com/RwAxB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RwAxB.png" alt="enter image description here" /></a></p>
  <p>I have a list of perhaps hundreds of unsorted 2D line segments at random angles as shown in blue above, and I need to determine the sorted sequence of points a – i from them as shown in red.  Here are some constraints and observations:</p>
  <ul>
  <li>The red polyline sequence a - i forms a tight enclosure of the outer
  points of all the segments.  It's like a convex hull, but &quot;sucked&quot;
  into concavities.  A tight enclosure of the entirety of all segments
  would be a fine solution; I can delete the unused parts.</li>
  <li>An enclosure does exist. The segments are not completely random.</li>
  <li>The y value of one point of each segment is 0, and the other end is a
  point on the enclosure.</li>
  <li>The enclosure cannot cross any line segment or itself.  This also
  means that all segments are on one side of the enclosure polyline.</li>
  <li>There will be either one or two enclosure segments connected to each
  enclosure point a - i.</li>
  <li>The points may be spaced widely differently, not as tidily as shown.</li>
  <li>I should be able to determine point a or i if necessary to start off
  the algorithm.</li>
  </ul>
  <p>It seems like starting from a bounding box around all segments and shrinking it to form a tight enclosure would be a reasonable approach, but I can't come up with a decent algorithm.  This will eventually be coded in C#, but an algorithm or pseudo-code would be fine.</p>