Statistics
| Revision:

root / tags / v2_0_0_Build_2047 / applications / appgvSIG / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / snapping / snappers / NearestPointSnapper.java @ 38317

History | View | Annotate | Download (2.92 KB)

1
package org.gvsig.app.project.documents.view.toolListeners.snapping.snappers;
2

    
3
import java.awt.geom.PathIterator;
4
import java.awt.geom.Point2D;
5

    
6
import org.gvsig.fmap.geom.Geometry;
7
import org.gvsig.fmap.mapcontrol.PrimitivesDrawer;
8
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperVectorial;
9
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.impl.AbstractSnapper;
10
import org.gvsig.i18n.Messages;
11

    
12
import com.vividsolutions.jts.geom.Coordinate;
13
import com.vividsolutions.jts.geom.LineSegment;
14

    
15
public class NearestPointSnapper extends AbstractSnapper implements ISnapperVectorial {
16
        public Point2D getSnapPoint(Point2D point, Geometry geom, double tolerance, Point2D lastPointEntered) {
17
                Point2D resul = null;
18
                Coordinate c = new Coordinate(point.getX(), point.getY());
19

    
20
                PathIterator theIterator = geom.getPathIterator(null, geomManager.getFlatness()); //polyLine.getPathIterator(null, flatness);
21
                double[] theData = new double[6];
22
                double minDist = tolerance;
23
                Coordinate from = null, first = null;
24
                while (!theIterator.isDone()) {
25
                        //while not done
26
                        int theType = theIterator.currentSegment(theData);
27

    
28
                        switch (theType) {
29
                                case PathIterator.SEG_MOVETO:
30
                                        from = new Coordinate(theData[0], theData[1]);
31
                                        first = from;
32
                                        break;
33

    
34
                                case PathIterator.SEG_LINETO:
35

    
36
                                        // System.out.println("SEG_LINETO");
37
                                        Coordinate to = new Coordinate(theData[0], theData[1]);
38
                                        LineSegment line = new LineSegment(from, to);
39
                                        Coordinate closestPoint = line.closestPoint(c);
40
                                        double dist = c.distance(closestPoint);
41
                                        if ((dist < minDist)) {
42
                                                resul = new Point2D.Double(closestPoint.x, closestPoint.y);
43
                                                minDist = dist;
44
                                        }
45

    
46
                                        from = to;
47
                                        break;
48
                                case PathIterator.SEG_CLOSE:
49
                                        line = new LineSegment(from, first);
50
                                        closestPoint = line.closestPoint(c);
51
                                        dist = c.distance(closestPoint);
52
                                        if ((dist < minDist)) {
53
                                                resul = new Point2D.Double(closestPoint.x, closestPoint.y);
54
                                                minDist = dist;
55
                                        }
56

    
57
                                        from = first;
58
                                        break;
59

    
60

    
61
                        } //end switch
62

    
63
                        theIterator.next();
64
                }
65

    
66
                return resul;
67
        }
68

    
69
        public String getToolTipText() {
70
                return Messages.getText("Nearest_point");
71
        }
72

    
73
        /*
74
         * (non-Javadoc)
75
         * @see org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper#draw(org.gvsig.fmap.mapcontrol.PrimitivesDrawer, java.awt.geom.Point2D)
76
         */
77
        public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) {
78
                primitivesDrawer.setColor(getColor());
79
                int half = getSizePixels() / 2;
80
                int x1 = (int) (pPixels.getX() - half);
81
                int x2 = (int) (pPixels.getX() + half);
82
                int y1 = (int) (pPixels.getY() - half);
83
                int y2 = (int) (pPixels.getY() + half);
84

    
85
                primitivesDrawer.drawLine(x1, y1, x2, y1); // abajo
86
                primitivesDrawer.drawLine(x1, y2, x2, y2); // arriba
87
                primitivesDrawer.drawLine(x1, y1, x2, y2); // abajo - arriba
88
                primitivesDrawer.drawLine(x1, y2, x2, y1); // arriba - abajo
89
        }
90
}