Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / snapping / snappers / PerpendicularPointSnapper.java @ 30349

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

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

    
14

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

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

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

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

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

    
53
                break;
54

    
55
            case PathIterator.SEG_LINETO:
56

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

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

    
72
                from = to;
73

    
74
                break;
75

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

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

    
90
                from = first;
91

    
92
                break;
93
            } //end switch
94

    
95
            theIterator.next();
96
        }
97

    
98
        return resul;
99
    }
100

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

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

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

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

    
129
}