コード例 #1
0
  /*
   * (non-Javadoc)
   *
   * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getSnapPoint(Point2D
   * point, IGeometry geom,double tolerance, Point2D lastPointEntered)
   */
  public Point2D getSnapPoint(
      Point2D point, IGeometry geom, double tolerance, Point2D lastPointEntered) {
    if (geom.getInternalShape() instanceof FCircle2D
        || geom.getInternalShape() instanceof FArc2D
        || geom.getInternalShape() instanceof FEllipse2D
        || geom.getInternalShape() instanceof FSpline2D) {
      return null;
    }

    Point2D resul = null;
    Coordinate c = new Coordinate(point.getX(), point.getY());

    PathIterator theIterator =
        geom.getPathIterator(null, FConverter.FLATNESS); // polyLine.getPathIterator(null,
    // flatness);
    double[] theData = new double[6];
    double minDist = tolerance;
    Coordinate from = null;
    Coordinate first = null;

    while (!theIterator.isDone()) {
      // while not done
      int theType = theIterator.currentSegment(theData);

      switch (theType) {
        case PathIterator.SEG_MOVETO:
          from = new Coordinate(theData[0], theData[1]);
          first = from;

          break;

        case PathIterator.SEG_LINETO:
          Coordinate to = new Coordinate(theData[0], theData[1]);
          Coordinate mediumPoint = new Coordinate((to.x + from.x) / 2, (to.y + from.y) / 2);
          double dist = c.distance(mediumPoint);

          if ((dist < minDist)) {
            resul = new Point2D.Double(mediumPoint.x, mediumPoint.y);
            minDist = dist;
          }

          from = to;

          break;

        case PathIterator.SEG_CLOSE:
          mediumPoint = new Coordinate((first.x + from.x) / 2, (first.y + from.y) / 2);
          dist = c.distance(mediumPoint);

          if ((dist < minDist)) {
            resul = new Point2D.Double(mediumPoint.x, mediumPoint.y);
            minDist = dist;
          }

          from = first;

          break;
      } // end switch

      theIterator.next();
    }

    return resul;
  }
コード例 #2
0
  /*
   * (non-Javadoc)
   *
   * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getSnapPoint(Point2D
   * point, IGeometry geom,double tolerance, Point2D lastPointEntered)
   */
  public Point2D getSnapPoint(
      Point2D point, IGeometry geom, double tolerance, Point2D lastPointEntered) {
    Point2D resul = null;
    Coordinate c = new Coordinate(point.getX(), point.getY());

    if (lastPointEntered == null) {
      return null;
    }

    Coordinate cLastPoint = new Coordinate(lastPointEntered.getX(), lastPointEntered.getY());
    PathIterator theIterator =
        geom.getPathIterator(null, FConverter.FLATNESS); // polyLine.getPathIterator(null,
    // flatness);
    double[] theData = new double[6];
    double minDist = tolerance;
    Coordinate from = null;
    Coordinate first = null;

    while (!theIterator.isDone()) {
      // while not done
      int theType = theIterator.currentSegment(theData);

      switch (theType) {
        case PathIterator.SEG_MOVETO:
          from = new Coordinate(theData[0], theData[1]);
          first = from;

          break;

        case PathIterator.SEG_LINETO:

          // System.out.println("SEG_LINETO");
          Coordinate to = new Coordinate(theData[0], theData[1]);
          LineSegment line = new LineSegment(from, to);
          Coordinate closestPoint = line.closestPoint(cLastPoint);
          double dist = c.distance(closestPoint);

          if (!(line.getCoordinate(0).equals2D(closestPoint)
              || line.getCoordinate(1).equals2D(closestPoint))) {
            if ((dist < minDist)) {
              resul = new Point2D.Double(closestPoint.x, closestPoint.y);
              minDist = dist;
            }
          }

          from = to;

          break;

        case PathIterator.SEG_CLOSE:
          line = new LineSegment(from, first);
          closestPoint = line.closestPoint(cLastPoint);
          dist = c.distance(closestPoint);

          if (!(line.getCoordinate(0).equals2D(closestPoint)
              || line.getCoordinate(1).equals2D(closestPoint))) {
            if ((dist < minDist)) {
              resul = new Point2D.Double(closestPoint.x, closestPoint.y);
              minDist = dist;
            }
          }

          from = first;

          break;
      } // end switch

      theIterator.next();
    }

    return resul;
  }