Exemple #1
0
  /*
   * Split the polygon in two, by the given line. Returns a list of polygons. The first is the one who contains the
   * given point.
   */
  public ArrayList<Polygon> getSplits(Point2D pointOnLeftSide, Line2D splitLine) {
    PointRing left = new PointRing();
    PointRing right = new PointRing();
    PointRing actual = left;

    // we check if the given point is valid for this polygon
    if (!points.contains(pointOnLeftSide))
      throw new RuntimeException("The specified point can't be found in this polygon.");

    Point2D p = pointOnLeftSide;
    Point2D start = null;

    // TODO, gerer les splits sur un point unique
    for (int i = 0; i < size(); i++) {
      actual.add(p);

      Point2D next = points.getNext(p);
      Segment2D edge = new Segment2D(p, next);
      if (edge.isCollinear(splitLine))
        throw new RuntimeException(
            "Trying to split a polygon with a line collinear to one of its edges : " + splitLine);
      if (edge.intersect(splitLine)) {
        // An intersection point is found
        Point2D intersection = edge.getAnyIntersection(splitLine);

        // We check if the intersection point is the end of the edge.
        // In this case, we continue to the next edge which will have intersection on its start
        // point.
        if (!intersection.equals(edge.getEnd())) {
          // We check if the intersection is on the edge start.
          // In this case, we don't add the point to the actual polygon
          if (!intersection.equals(edge.getStart())) actual.add(intersection);
          // then we switch actual polygon
          if (actual == left) {
            // we save the point just before the intersection. after the split, we will shift the
            // left
            // polygon to this point.
            start = p;
            actual = right;
          } else actual = left;

          // after switching, we add the intersection to the new actual polygon.
          actual.add(intersection);
        }
      }
      p = next;
    }
    // debug : check if we get only one intersection
    if (actual == right)
      throw new RuntimeException(
          "The polygon have been splitted on an impair number of points, which is not allowed.");

    left.shiftTo(start);

    ArrayList<Polygon> res = new ArrayList<Polygon>();
    res.add(new Polygon(left));
    res.add(new Polygon(right));
    return res;
  }