Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / view / snapping / snappers / PerpendicularPointSnapper.java @ 8943

History | View | Annotate | Download (4.41 KB)

1
package com.iver.cit.gvsig.project.documents.view.snapping.snappers;
2

    
3
import com.iver.andami.PluginServices;
4

    
5
import com.iver.cit.gvsig.fmap.core.IGeometry;
6
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
7
import com.iver.cit.gvsig.project.documents.view.snapping.AbstractSnapper;
8
import com.iver.cit.gvsig.project.documents.view.snapping.ISnapperVectorial;
9

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

    
13
import java.awt.Graphics;
14
import java.awt.geom.PathIterator;
15
import java.awt.geom.Point2D;
16

    
17

    
18
/**
19
 * Perpendicular point snapper.
20
 *
21
 * @author Vicente Caballero Navarro
22
 */
23
public class PerpendicularPointSnapper extends AbstractSnapper
24
    implements ISnapperVectorial {
25

    
26
        /* (non-Javadoc)
27
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getSnapPoint(Point2D point,
28
     * IGeometry geom,double tolerance, Point2D lastPointEntered)
29
     */
30
    public Point2D getSnapPoint(Point2D point, IGeometry geom,
31
        double tolerance, Point2D lastPointEntered) {
32
        Point2D resul = null;
33
        Coordinate c = new Coordinate(point.getX(), point.getY());
34

    
35
        if (lastPointEntered == null) {
36
            return null;
37
        }
38

    
39
        Coordinate cLastPoint = new Coordinate(lastPointEntered.getX(),
40
                lastPointEntered.getY());
41
        PathIterator theIterator = geom.getPathIterator(null,
42
                FConverter.FLATNESS); //polyLine.getPathIterator(null, flatness);
43
        double[] theData = new double[6];
44
        double minDist = tolerance;
45
        Coordinate from = null;
46
        Coordinate first = null;
47

    
48
        while (!theIterator.isDone()) {
49
            //while not done
50
            int theType = theIterator.currentSegment(theData);
51

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

    
57
                break;
58

    
59
            case PathIterator.SEG_LINETO:
60

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

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

    
76
                from = to;
77

    
78
                break;
79

    
80
            case PathIterator.SEG_CLOSE:
81
                line = new LineSegment(from, first);
82
                closestPoint = line.closestPoint(cLastPoint);
83
                dist = c.distance(closestPoint);
84

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

    
94
                from = first;
95

    
96
                break;
97
            } //end switch
98

    
99
            theIterator.next();
100
        }
101

    
102
        return resul;
103
    }
104

    
105
    /* (non-Javadoc)
106
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getToolTipText()
107
     */
108
    public String getToolTipText() {
109
        return PluginServices.getText(this, "perpendicular_point");
110
    }
111

    
112
    /* (non-Javadoc)
113
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#draw(java.awt.Graphics, java.awt.geom.Point2D)
114
     */
115
    public void draw(Graphics g, Point2D pPixels) {
116
        g.setColor(getColor());
117

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

    
126
        g.drawLine(x1, y2, x2, y2);
127
        g.drawLine(x1, y2, x1, y1);
128
        g.drawLine(x1, y3, x3, y3);
129
        g.drawLine(x3, y3, x3, y2);
130
    }
131

    
132
    /* (non-Javadoc)
133
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getPriority()
134
     */
135
    public int getPriority() {
136
        return 9;
137
    }
138
}