Statistics
| Revision:

root / tags / v2_0_0_Build_2049 / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / snapping / snappers / PerpendicularPointSnapper.java @ 38460

History | View | Annotate | Download (4.48 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

    
16
/**
17
 * Perpendicular point snapper.
18
 *
19
 * @author Vicente Caballero Navarro
20
 */
21
public class PerpendicularPointSnapper extends AbstractSnapper
22
    implements ISnapperVectorial {
23
        /* (non-Javadoc)
24
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getSnapPoint(Point2D point,
25
     * IGeometry geom,double tolerance, Point2D lastPointEntered)
26
     */
27
    public Point2D getSnapPoint(Point2D point, Geometry geom,
28
        double tolerance, Point2D lastPointEntered) {
29
        Point2D resul = null;
30
        Coordinate c = new Coordinate(point.getX(), point.getY());
31

    
32
        if (lastPointEntered == null) {
33
            return null;
34
        }
35

    
36
        Coordinate cLastPoint = new Coordinate(lastPointEntered.getX(),
37
                lastPointEntered.getY());
38
        PathIterator theIterator = geom.getPathIterator(null,
39
                geomManager.getFlatness()); //polyLine.getPathIterator(null, flatness);
40
        double[] theData = new double[6];
41
        double minDist = tolerance;
42
        Coordinate from = null;
43
        Coordinate first = null;
44

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

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

    
54
                break;
55

    
56
            case PathIterator.SEG_LINETO:
57

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

    
64
                if (!(line.getCoordinate(0).equals2D(closestPoint) ||
65
                        line.getCoordinate(1).equals2D(closestPoint))) {
66
                    if ((dist < minDist)) {
67
                        resul = new Point2D.Double(closestPoint.x,
68
                                closestPoint.y);
69
                        minDist = dist;
70
                    }
71
                }
72

    
73
                from = to;
74

    
75
                break;
76

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

    
82
                if (!(line.getCoordinate(0).equals2D(closestPoint) ||
83
                        line.getCoordinate(1).equals2D(closestPoint))) {
84
                    if ((dist < minDist)) {
85
                        resul = new Point2D.Double(closestPoint.x,
86
                                closestPoint.y);
87
                        minDist = dist;
88
                    }
89
                }
90

    
91
                from = first;
92

    
93
                break;
94
            } //end switch
95

    
96
            theIterator.next();
97
        }
98

    
99
        return resul;
100
    }
101

    
102
    /* (non-Javadoc)
103
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getToolTipText()
104
     */
105
    public String getToolTipText() {
106
        return Messages.getText("Perpendicular_point");
107
    }
108

    
109
    /*
110
     * (non-Javadoc)
111
     * @see org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper#draw(org.gvsig.fmap.mapcontrol.PrimitivesDrawer, java.awt.geom.Point2D)
112
     */
113
    public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) {
114
            primitivesDrawer.setColor(getColor());
115

    
116
        int half = getSizePixels() / 2;
117
        int x1 = (int) (pPixels.getX() - half);
118
        int x2 = (int) (pPixels.getX() + half);
119
        int x3 = (int) pPixels.getX();
120
        int y1 = (int) (pPixels.getY() - half);
121
        int y2 = (int) (pPixels.getY() + half);
122
        int y3 = (int) pPixels.getY();
123

    
124
        primitivesDrawer.drawLine(x1, y2, x2, y2);
125
        primitivesDrawer.drawLine(x1, y2, x1, y1);
126
        primitivesDrawer.drawLine(x1, y3, x3, y3);
127
        primitivesDrawer.drawLine(x3, y3, x3, y2);
128
    }
129

    
130
}