Statistics
| Revision:

root / tags / v1_0_2_Build_915 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / snapping / NearestPointSnapper.java @ 12217

History | View | Annotate | Download (2.72 KB)

1
package com.iver.cit.gvsig.gui.cad.snapping;
2

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

    
7
import com.iver.andami.PluginServices;
8
import com.iver.cit.gvsig.fmap.core.IGeometry;
9
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
10
import com.vividsolutions.jts.geom.Coordinate;
11
import com.vividsolutions.jts.geom.LineSegment;
12

    
13
public class NearestPointSnapper extends AbstractSnapper implements ISnapperVectorial {
14

    
15
        public Point2D getSnapPoint(Point2D point, IGeometry geom, double tolerance, Point2D lastPointEntered) {
16
                Point2D resul = null;
17
                Coordinate c = new Coordinate(point.getX(), point.getY());
18
                
19
                PathIterator theIterator = geom.getPathIterator(null, FConverter.FLATNESS); //polyLine.getPathIterator(null, flatness);
20
                double[] theData = new double[6];
21
                double minDist = tolerance;
22
                Coordinate from = null, first = null;
23
                while (!theIterator.isDone()) {
24
                        //while not done
25
                        int theType = theIterator.currentSegment(theData);
26

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

    
33
                                case PathIterator.SEG_LINETO:
34

    
35
                                        // System.out.println("SEG_LINETO");
36
                                        Coordinate to = new Coordinate(theData[0], theData[1]);
37
                                        LineSegment line = new LineSegment(from, to);
38
                                        Coordinate closestPoint = line.closestPoint(c);
39
                                        double dist = c.distance(closestPoint);
40
                                        if ((dist < minDist)) {
41
                                                resul = new Point2D.Double(closestPoint.x, closestPoint.y);
42
                                                minDist = dist;
43
                                        }
44
                                        
45
                                        from = to;
46
                                        break;
47
                                case PathIterator.SEG_CLOSE:
48
                                        line = new LineSegment(from, first);
49
                                        closestPoint = line.closestPoint(c);
50
                                        dist = c.distance(closestPoint);
51
                                        if ((dist < minDist)) {
52
                                                resul = new Point2D.Double(closestPoint.x, closestPoint.y);
53
                                                minDist = dist;
54
                                        }
55
                                        
56
                                        from = first;
57
                                        break;
58
                                        
59

    
60
                        } //end switch
61

    
62
                        theIterator.next();
63
                }
64

    
65
                return resul;
66
        }
67

    
68
        public String getToolTipText() {
69
                return PluginServices.getText(this, "nearest_point");
70
        }
71

    
72
        /* (non-Javadoc)
73
         * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#draw(java.awt.Graphics, java.awt.geom.Point2D)
74
         */
75
        public void draw(Graphics g, Point2D pPixels) {
76
                g.setColor(getColor());        
77
                int half = getSizePixels() / 2;
78
                int x1 = (int) (pPixels.getX() - half);
79
                int x2 = (int) (pPixels.getX() + half);
80
                int y1 = (int) (pPixels.getY() - half);
81
                int y2 = (int) (pPixels.getY() + half);
82
                
83
                g.drawLine(x1, y1, x2, y1); // abajo
84
                g.drawLine(x1, y2, x2, y2); // arriba
85
                g.drawLine(x1, y1, x2, y2); // abajo - arriba
86
                g.drawLine(x1, y2, x2, y1); // arriba - abajo
87
        }
88
        
89
        /* (non-Javadoc)
90
         * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getPriority()
91
         */
92
        public int getPriority()
93
        {
94
                return 0;
95
        }
96

    
97
}