docs: public LineSegment clipToRectangle(Rectangle r) {
  
          Point a = this.start;
          Point b = this.end;
  
          int codeStart = code(r, a);
          int codeEnd = code(r, b);
  
          while (true) {
              if (0 == (codeStart | codeEnd)) {
                  // both points are inside, intersection is the computed line
                  return new LineSegment(a, b);
              } else if (0 != (codeStart & codeEnd)) {
                  // both points are either below, above, left or right of the box, no intersection
                  return null;
              } else {
                  double newX;
                  double newY;
                  // At least one endpoint is outside the clip rectangle; pick it.
                  int outsideCode = (0 != codeStart) ? codeStart : codeEnd;
  
                  if (0 != (outsideCode & TOP)) {
                      // point is above the clip rectangle
                      newX = a.x + (b.x - a.x) * (r.top - a.y) / (b.y - a.y);
                      newY = r.top;
                  } else if (0 != (outsideCode & BOTTOM)) {
                      // point is below the clip rectangle
                      newX = a.x + (b.x - a.x) * (r.bottom - a.y) / (b.y - a.y);
                      newY = r.bottom;
                  } else if (0 != (outsideCode & RIGHT)) {
                      // point is to the right of clip rectangle
                      newY = a.y + (b.y - a.y) * (r.right - a.x) / (b.x - a.x);
                      newX = r.right;
                  } else if (0 != (outsideCode & LEFT)) {
                      // point is to the left of clip rectangle
                      newY = a.y + (b.y - a.y) * (r.left - a.x) / (b.x - a.x);
                      newX = r.left;
                  } else {
                      throw new IllegalStateException("Should not get here");
                  }
                  // Now we move outside point to intersection point to clip
                  // and get ready for next pass.
                  if (outsideCode == codeStart) {
                      a = new Point(newX, newY);
                      codeStart = code(r, a);
                  } else {
                      b = new Point(newX, newY);
                      codeEnd = code(r, b);
                  }
              }
          }
      }