Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / view / snapping / snappers / PerpendicularPointSnapper.java @ 10626

History | View | Annotate | Download (4.26 KB)

1
package com.iver.cit.gvsig.project.documents.view.snapping.snappers;
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.iver.cit.gvsig.project.documents.view.snapping.AbstractSnapper;
11
import com.iver.cit.gvsig.project.documents.view.snapping.ISnapperVectorial;
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, IGeometry 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
                FConverter.FLATNESS); //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 PluginServices.getText(this, "perpendicular_point");
107
    }
108

    
109
    /* (non-Javadoc)
110
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#draw(java.awt.Graphics, java.awt.geom.Point2D)
111
     */
112
    public void draw(Graphics g, Point2D pPixels) {
113
        g.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
        g.drawLine(x1, y2, x2, y2);
124
        g.drawLine(x1, y2, x1, y1);
125
        g.drawLine(x1, y3, x3, y3);
126
        g.drawLine(x3, y3, x3, y2);
127
    }
128

    
129
}