Beispiel #1
0
 private void generateAnotherCell(Graphics g, int dx, int dy) {
   Polygon polygon = new Polygon();
   AffineTransform at = AffineTransform.getTranslateInstance(dx, dy);
   PathIterator pointsIterator = center.getPathIterator(at);
   while (!pointsIterator.isDone()) {
     double[] xy = new double[2];
     pointsIterator.currentSegment(xy);
     if (xy[0] == 0 && xy[1] == 0) {
       break;
     }
     polygon.addPoint((int) xy[0], (int) xy[1]);
     pointsIterator.next();
   }
   g.fillPolygon(polygon);
 }
Beispiel #2
0
  /**
   * @param poly main cluster Polygon
   * @param line sub cluster polygon lines
   * @return Set of intersect Point
   * @throws Exception e
   */
  private Set<Point2D> getIntersections(final Polygon poly, final Line2D.Double line)
      throws Exception {

    final PathIterator polyIt =
        poly.getPathIterator(null); // Getting an iterator along the polygon path
    final double[] coords = new double[6]; // Double array with length 6 needed by iterator
    final double[] firstCoords = new double[2]; // First point (needed for closing polygon path)
    final double[] lastCoords = new double[2]; // Previously visited point
    final Set<Point2D> intersections = new HashSet<Point2D>(); // List to hold found intersections
    polyIt.currentSegment(firstCoords); // Getting the first coordinate pair
    lastCoords[0] = firstCoords[0]; // Priming the previous coordinate pair
    lastCoords[1] = firstCoords[1];
    polyIt.next();
    while (!polyIt.isDone()) {
      final int type = polyIt.currentSegment(coords);
      switch (type) {
        case PathIterator.SEG_LINETO:
          {
            final Line2D.Double currentLine =
                new Line2D.Double(lastCoords[0], lastCoords[1], coords[0], coords[1]);
            if (currentLine.intersectsLine(line)) {
              intersections.add(getIntersection(currentLine, line));
            }
            lastCoords[0] = coords[0];
            lastCoords[1] = coords[1];
            break;
          }
        case PathIterator.SEG_CLOSE:
          {
            final Line2D.Double currentLine =
                new Line2D.Double(coords[0], coords[1], firstCoords[0], firstCoords[1]);
            if (currentLine.intersectsLine(line)) {
              intersections.add(getIntersection(currentLine, line));
            }
            break;
          }
        default:
          {
            throw new Exception("Unsupported PathIterator segment type.");
          }
      }
      polyIt.next();
    }
    return intersections;
  }