Example #1
0
    public void actionPerformed(ActionEvent ae) {
      Area area = null;
      List<Attribute> attrs = new ArrayList<Attribute>();
      boolean fill = false;
      for (Vector v : shapes) {
        Primitive vv = v;
        while (vv instanceof Proxy && !(vv instanceof PaintedPrimitive)) {
          vv = ((Proxy) vv).getProxiedPrimitive();
        }
        if (vv instanceof PaintedPrimitive) {
          PaintedPrimitive pp = (PaintedPrimitive) vv;
          List<Attribute> atts = pp.allAttributes();
          System.err.println("  include attributes " + atts);
          attrs.addAll(pp.allAttributes());
        }
        fill |= v instanceof Fillable && ((Fillable) v).isFill();
        if (area == null) {
          area = new Area(v.toShape());
        } else {
          Area other = new Area(v.toShape());
          switch (kind) {
            case UNION:
              area.add(other);
              break;
            case INTERSECTION:
              area.intersect(other);
              break;
            case SUBTRACTION:
              area.subtract(other);
              break;
            case XOR:
              area.exclusiveOr(other);
              break;
            default:
              throw new AssertionError();
          }
        }
      }
      if (area != null) {
        PathIteratorWrapper wrap =
            new PathIteratorWrapper(
                area.getPathIterator(AffineTransform.getTranslateInstance(0, 0)), fill);
        // XXX probably don't want all attributes, they may be
        // redundant
        PaintedPrimitive pp = PaintedPrimitive.create(wrap, attrs);

        stack.replace(shapes, pp);
        if (repainter != null) {
          repainter.requestRepaint();
          ;
        }
      }
    }
Example #2
0
  public NewPolygon2D clip(Rectangle2D boundingBox) {
    Area thisArea = new Area(path);
    thisArea.intersect(new Area(boundingBox));
    PathIterator iterator = thisArea.getPathIterator(null);
    double[] v = new double[2];
    while (!iterator.isDone()) {
      int type = iterator.currentSegment(v);
      System.err.println(":" + v[0] + v[1] + "\n");
      iterator.next();
    }
    System.exit(-1);

    GeneralPath path = new GeneralPath(thisArea);
    path.closePath();
    NewPolygon2D newPolygon = new NewPolygon2D(path);
    newPolygon.setFillValue(this.getFillValue());
    return newPolygon;
  }
Example #3
0
    private int computeHashCode() {
      int hash = width * 1313 + height * 71;
      // Area.hashCode() is not implemented, so we have to do it ourselves..
      PathIterator pi = area.getPathIterator(null);
      double[] arg = new double[6];
      while (!pi.isDone()) {
        for (int i = 0; i < 6; i++) {
          arg[i] = 0;
        }

        int val = pi.getWindingRule();
        val += 3 * pi.currentSegment(arg);
        for (int i = 0; i < 6; i++) {
          val = (val * 5) + (int) Math.floor(arg[i] * 1000);
        }

        hash = hash * 7 + val;
        pi.next();
      }

      return hash;
    }
Example #4
0
    /**
     * Converts an awt Area to a String representing an SVG path.
     *
     * @param a The Area to convert to an SVG path.
     * @returns An SVG specification of the passed in Area.
     */
    protected static String toSVGPath(Area a) {
      StringBuilder sb = new StringBuilder();

      PathIterator it = a.getPathIterator(null);
      if (null == it) {
        return new String();
      }

      // PathIterator is not a normal Java Iterator
      while (!it.isDone()) {
        double[] c = new double[6];

        switch (it.currentSegment(c)) {
          case PathIterator.SEG_MOVETO:
            sb.append(String.format("M%.2f,%.2f ", c[0], c[1]));
            break;
          case PathIterator.SEG_LINETO:
            sb.append(String.format("L%.2f,%.2f ", c[0], c[1]));
            break;
          case PathIterator.SEG_QUADTO:
            sb.append(String.format("Q%.2f,%.2f,%.2f,%.2f ", c[0], c[1], c[2], c[3]));
            break;
          case PathIterator.SEG_CUBICTO:
            sb.append(
                String.format(
                    "C%.2f,%.2f,%.2f,%.2f,%.2f,%.2f ", c[0], c[1], c[2], c[3], c[4], c[5]));
            break;
          case PathIterator.SEG_CLOSE:
            sb.append("Z");
            break;
        }

        // update
        it.next();
      }
      return sb.toString();
    }
  protected final void compute() {

    ArrayList<Double> xcoord = new ArrayList<Double>();
    ArrayList<Double> ycoord = new ArrayList<Double>();
    double[] coords = new double[6];
    double[] oldCoords = new double[6];

    // Convert input polygons to Area objects
    Area a1 = getArea(inPoly0.getPoints());
    Area a2 = getArea(inPoly1.getPoints());

    // test for empty intersection
    Area testArea = getArea(inPoly0.getPoints());
    testArea.intersect(a2);
    if (testArea.isEmpty()) {
      poly.setUndefined();
    }
    // if intersection is non-empty perform operation
    else {
      switch (operationType) {
        case TYPE_INTERSECTION:
          a1.intersect(a2);
          break;
        case TYPE_UNION:
          a1.add(a2);
          break;
        case TYPE_DIFFERENCE:
          a1.subtract(a2);
          break;
      }

      // Iterate through the path of the result
      // and recover the polygon vertices.

      PathIterator it = a1.getPathIterator(null);

      int type = it.currentSegment(coords);
      it.next();
      oldCoords = coords.clone();
      double epsilon = 1E-10;

      while (!it.isDone()) {
        type = it.currentSegment(coords);
        if (type == PathIterator.SEG_CLOSE) {
          break;
        }
        // Sometimes the Path iterator gives two almost identical points and
        // we only want one of them.
        // TODO: Why does this happen???
        if ((double) Math.abs(oldCoords[0] - coords[0]) > epsilon
            || (double) Math.abs(oldCoords[1] - coords[1]) > epsilon) {
          xcoord.add(coords[0]);
          ycoord.add(coords[1]);
        }
        oldCoords = coords.clone();

        it.next();
      }
    }

    // Update the points array to the correct size
    int n = xcoord.size();
    // System.out.println("number of points: " + n);
    int oldPointNumber = points.length;
    if (n != oldPointNumber) {
      updatePointsArray(n);
      poly.setPoints(points);
      setOutput();
    }

    // Set the points to the new polygon vertices
    for (int k = 0; k < n; k++) {
      points[k].setCoords(xcoord.get(k), ycoord.get(k), 1);
      // System.out.println("vertices: " + xcoord.get(k) + " , " + ycoord.get(k));

    }

    // Compute area of poly (this will also set our poly geo to be defined)
    poly.calcArea();

    // update new points and segments
    if (n != oldPointNumber) {
      updateSegmentsAndPointsLabels(oldPointNumber);
    }
  }