Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toolListeners / snapping / snappers / TangentPointSnapper.java @ 27035

History | View | Annotate | Download (3.88 KB)

1 23642 vcaballero
package com.iver.cit.gvsig.project.documents.view.toolListeners.snapping.snappers;
2
3
import java.awt.Graphics;
4
import java.awt.geom.PathIterator;
5
import java.awt.geom.Point2D;
6
7
import org.gvsig.fmap.geom.Geometry;
8 27035 jpiera
import org.gvsig.fmap.geom.primitive.Arc;
9
import org.gvsig.fmap.geom.primitive.Circle;
10
import org.gvsig.fmap.geom.primitive.Ellipse;
11
import org.gvsig.fmap.geom.primitive.Spline;
12 23642 vcaballero
import org.gvsig.fmap.geom.util.Converter;
13
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.AbstractSnapper;
14
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperVectorial;
15
16
import com.vividsolutions.jts.geom.Coordinate;
17
import com.vividsolutions.jts.geom.LineSegment;
18
19
20
/**
21
 * Tangent point snapper.
22
 *
23
 * @author Vicente Caballero Navarro
24
 */
25
public class TangentPointSnapper extends AbstractSnapper
26
    implements ISnapperVectorial {
27
        /* (non-Javadoc)
28
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getSnapPoint(Point2D point,
29
     * IGeometry geom,double tolerance, Point2D lastPointEntered)
30
     */
31
    public Point2D getSnapPoint(Point2D point, Geometry geom,
32
        double tolerance, Point2D lastPointEntered) {
33 27035 jpiera
        if (!(geom instanceof Circle ||
34
                geom instanceof Arc ||
35
                geom instanceof Ellipse ||
36
                geom instanceof Spline)) {
37 23642 vcaballero
            return null;
38
        }
39
40
        Point2D resul = null;
41
        Coordinate c = new Coordinate(point.getX(), point.getY());
42
43
        PathIterator theIterator = geom.getPathIterator(null,
44
                Converter.FLATNESS);
45
        double[] theData = new double[6];
46
        double minDist = tolerance;
47
        Coordinate from = null;
48
        Coordinate first = null;
49
50
        while (!theIterator.isDone()) {
51
            //while not done
52
            int theType = theIterator.currentSegment(theData);
53
54
            switch (theType) {
55
            case PathIterator.SEG_MOVETO:
56
                from = new Coordinate(theData[0], theData[1]);
57
                first = from;
58
59
                break;
60
61
            case PathIterator.SEG_LINETO:
62
63
                // System.out.println("SEG_LINETO");
64
                Coordinate to = new Coordinate(theData[0], theData[1]);
65
                LineSegment line = new LineSegment(from, to);
66
                Coordinate closestPoint = line.closestPoint(c);
67
                double dist = c.distance(closestPoint);
68
69
                if ((dist < minDist)) {
70
                    resul = new Point2D.Double(closestPoint.x, closestPoint.y);
71
                    minDist = dist;
72
                }
73
74
                from = to;
75
76
                break;
77
78
            case PathIterator.SEG_CLOSE:
79
                line = new LineSegment(from, first);
80
                closestPoint = line.closestPoint(c);
81
                dist = c.distance(closestPoint);
82
83
                if ((dist < minDist)) {
84
                    resul = new Point2D.Double(closestPoint.x, closestPoint.y);
85
                    minDist = dist;
86
                }
87
88
                from = first;
89
90
                break;
91
            } //end switch
92
93
            theIterator.next();
94
        }
95
96
        return resul;
97
    }
98
99
    /* (non-Javadoc)
100
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getToolTipText()
101
     */
102
    public String getToolTipText() {
103
        return "tangent_point";
104
    }
105
106
    /* (non-Javadoc)
107
     * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#draw(java.awt.Graphics, java.awt.geom.Point2D)
108
     */
109
    public void draw(Graphics g, Point2D pPixels) {
110
        g.setColor(getColor());
111
112
        int half = getSizePixels() / 2;
113
        g.drawLine((int) (pPixels.getX() - half),
114
            (int) (pPixels.getY() - half), (int) (pPixels.getX() + half),
115
            (int) (pPixels.getY() - half));
116
        g.drawOval((int) (pPixels.getX() - half),
117
            (int) (pPixels.getY() - half), getSizePixels(), getSizePixels());
118
    }
119
120
121
}