code: private Segment segupdate(double xend, double yend, int dir, boolean tail, Segment[] seg) {
          Segment seglist = seg[0];
          if (seglist == null) {
              seg[1] = null;
              return null;
          }
  
          switch (dir) {
              case 1:
              case 3:
                  if (YMATCH(yend, seglist.y0)) {
                      if (!tail) {
                          seglist.swap();
                      }
                      seg[1] = seglist;
                      return seglist.next;
                  }
  
                  if (YMATCH(yend, seglist.y1)) {
                      if (tail) {
                          seglist.swap();
                      }
                      seg[1] = seglist;
                      return seglist.next;
                  }
  
                  if (YMATCH(yend, seglist.y1)) {
                      if (tail) {
                          seglist.swap();
                      }
                      seg[1] = seglist;
                      return seglist.next;
                  }
                  break;
  
              case 2:
              case 4:
                  if (XMATCH(xend, seglist.x0)) {
                      if (!tail) {
                          seglist.swap();
                      }
                      seg[1] = seglist;
                      return seglist.next;
                  }
  
                  if (XMATCH(xend, seglist.x1)) {
                      if (tail) {
                          seglist.swap();
                      }
                      seg[1] = seglist;
                      return seglist.next;
                  }
                  break;
          }
  
          Segment[] seg2 = {seglist.next, seg[1]};
          seglist.next = segupdate(xend, yend, dir, tail, seg2);
          seg[1] = seg2[1];
  
          return seglist;
      }
docs: Search seglist for a segment with endpoint (xend, yend).
  The cell entry direction is dir, and if tail=1/0 we are
  building the tail/head of a contour. The matching segment
  is pointed to by seg and the updated segment list (with
  the matched segment stripped) is returned by the function.