Statistics
| Revision:

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

History | View | Annotate | Download (3.93 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.FArc2D;
9
import com.iver.cit.gvsig.fmap.core.FCircle2D;
10
import com.iver.cit.gvsig.fmap.core.FEllipse2D;
11
import com.iver.cit.gvsig.fmap.core.FSpline2D;
12
import com.iver.cit.gvsig.fmap.core.IGeometry;
13
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
14
import com.iver.cit.gvsig.project.documents.view.snapping.AbstractSnapper;
15
import com.iver.cit.gvsig.project.documents.view.snapping.ISnapperVectorial;
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, IGeometry geom,
32
        double tolerance, Point2D lastPointEntered) {
33
        if (!(geom.getInternalShape() instanceof FCircle2D ||
34
                geom.getInternalShape() instanceof FArc2D ||
35
                geom.getInternalShape() instanceof FEllipse2D ||
36
                geom.getInternalShape() instanceof FSpline2D)) {
37
            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
                FConverter.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 PluginServices.getText(this, "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
}