Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toolListeners / snapping / snappers / NearestPointSnapper.java @ 40558

History | View | Annotate | Download (3.88 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.project.documents.view.toolListeners.snapping.snappers;
25

    
26
import java.awt.geom.PathIterator;
27
import java.awt.geom.Point2D;
28

    
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.mapcontrol.PrimitivesDrawer;
31
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperVectorial;
32
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.impl.AbstractSnapper;
33
import org.gvsig.i18n.Messages;
34

    
35
import com.vividsolutions.jts.geom.Coordinate;
36
import com.vividsolutions.jts.geom.LineSegment;
37

    
38
public class NearestPointSnapper extends AbstractSnapper implements ISnapperVectorial {
39
        public Point2D getSnapPoint(Point2D point, Geometry geom, double tolerance, Point2D lastPointEntered) {
40
                Point2D resul = null;
41
                Coordinate c = new Coordinate(point.getX(), point.getY());
42

    
43
                PathIterator theIterator = geom.getPathIterator(null, geomManager.getFlatness()); //polyLine.getPathIterator(null, flatness);
44
                double[] theData = new double[6];
45
                double minDist = tolerance;
46
                Coordinate from = null, first = null;
47
                while (!theIterator.isDone()) {
48
                        //while not done
49
                        int theType = theIterator.currentSegment(theData);
50

    
51
                        switch (theType) {
52
                                case PathIterator.SEG_MOVETO:
53
                                        from = new Coordinate(theData[0], theData[1]);
54
                                        first = from;
55
                                        break;
56

    
57
                                case PathIterator.SEG_LINETO:
58

    
59
                                        // System.out.println("SEG_LINETO");
60
                                        Coordinate to = new Coordinate(theData[0], theData[1]);
61
                                        LineSegment line = new LineSegment(from, to);
62
                                        Coordinate closestPoint = line.closestPoint(c);
63
                                        double dist = c.distance(closestPoint);
64
                                        if ((dist < minDist)) {
65
                                                resul = new Point2D.Double(closestPoint.x, closestPoint.y);
66
                                                minDist = dist;
67
                                        }
68

    
69
                                        from = to;
70
                                        break;
71
                                case PathIterator.SEG_CLOSE:
72
                                        line = new LineSegment(from, first);
73
                                        closestPoint = line.closestPoint(c);
74
                                        dist = c.distance(closestPoint);
75
                                        if ((dist < minDist)) {
76
                                                resul = new Point2D.Double(closestPoint.x, closestPoint.y);
77
                                                minDist = dist;
78
                                        }
79

    
80
                                        from = first;
81
                                        break;
82

    
83

    
84
                        } //end switch
85

    
86
                        theIterator.next();
87
                }
88

    
89
                return resul;
90
        }
91

    
92
        public String getToolTipText() {
93
                return Messages.getText("Nearest_point");
94
        }
95

    
96
        /*
97
         * (non-Javadoc)
98
         * @see org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper#draw(org.gvsig.fmap.mapcontrol.PrimitivesDrawer, java.awt.geom.Point2D)
99
         */
100
        public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) {
101
                primitivesDrawer.setColor(getColor());
102
                int half = getSizePixels() / 2;
103
                int x1 = (int) (pPixels.getX() - half);
104
                int x2 = (int) (pPixels.getX() + half);
105
                int y1 = (int) (pPixels.getY() - half);
106
                int y2 = (int) (pPixels.getY() + half);
107

    
108
                primitivesDrawer.drawLine(x1, y1, x2, y1); // abajo
109
                primitivesDrawer.drawLine(x1, y2, x2, y2); // arriba
110
                primitivesDrawer.drawLine(x1, y1, x2, y2); // abajo - arriba
111
                primitivesDrawer.drawLine(x1, y2, x2, y1); // arriba - abajo
112
        }
113
}