class CommonAlgorithmsFromCartAGen {

  public static ILineString insertVertex(ILineString line,
      IDirectPosition newVertex) {
    if (line.coord().contains(newVertex))
      return line;
    IDirectPositionList pts = new DirectPositionList();
    pts.add(line.startPoint());
    List<Segment> segments = Segment.getSegmentList(line);
    boolean added = false;
    for (int i = 0; i < segments.size(); i++) {
      Segment seg = segments.get(i);
      if (!seg.containsPoint(newVertex) || added) {
        pts.add(seg.endPoint());
        continue;
      }
      pts.add(newVertex);
      pts.add(seg.endPoint());
      added = true;
    }
    return new GM_LineString(pts);
  }

}