root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / view / snapping / snappers / TangentPointSnapper.java @ 8943
History | View | Annotate | Download (4.09 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.FArc2D; |
6 |
import com.iver.cit.gvsig.fmap.core.FCircle2D; |
7 |
import com.iver.cit.gvsig.fmap.core.FEllipse2D; |
8 |
import com.iver.cit.gvsig.fmap.core.FSpline2D; |
9 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
10 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
11 |
import com.iver.cit.gvsig.project.documents.view.snapping.AbstractSnapper; |
12 |
import com.iver.cit.gvsig.project.documents.view.snapping.ISnapperVectorial; |
13 |
|
14 |
import com.vividsolutions.jts.geom.Coordinate; |
15 |
import com.vividsolutions.jts.geom.LineSegment; |
16 |
|
17 |
import java.awt.Graphics; |
18 |
import java.awt.geom.PathIterator; |
19 |
import java.awt.geom.Point2D; |
20 |
|
21 |
|
22 |
/**
|
23 |
* Tangent point snapper.
|
24 |
*
|
25 |
* @author Vicente Caballero Navarro
|
26 |
*/
|
27 |
public class TangentPointSnapper extends AbstractSnapper |
28 |
implements ISnapperVectorial {
|
29 |
|
30 |
/* (non-Javadoc)
|
31 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getSnapPoint(Point2D point,
|
32 |
* IGeometry geom,double tolerance, Point2D lastPointEntered)
|
33 |
*/
|
34 |
public Point2D getSnapPoint(Point2D point, IGeometry geom, |
35 |
double tolerance, Point2D lastPointEntered) { |
36 |
if (!(geom.getInternalShape() instanceof FCircle2D || |
37 |
geom.getInternalShape() instanceof FArc2D ||
|
38 |
geom.getInternalShape() instanceof FEllipse2D ||
|
39 |
geom.getInternalShape() instanceof FSpline2D)) {
|
40 |
return null; |
41 |
} |
42 |
|
43 |
Point2D resul = null; |
44 |
Coordinate c = new Coordinate(point.getX(), point.getY());
|
45 |
|
46 |
PathIterator theIterator = geom.getPathIterator(null, |
47 |
FConverter.FLATNESS); |
48 |
double[] theData = new double[6]; |
49 |
double minDist = tolerance;
|
50 |
Coordinate from = null;
|
51 |
Coordinate first = null;
|
52 |
|
53 |
while (!theIterator.isDone()) {
|
54 |
//while not done
|
55 |
int theType = theIterator.currentSegment(theData);
|
56 |
|
57 |
switch (theType) {
|
58 |
case PathIterator.SEG_MOVETO: |
59 |
from = new Coordinate(theData[0], theData[1]); |
60 |
first = from; |
61 |
|
62 |
break;
|
63 |
|
64 |
case PathIterator.SEG_LINETO: |
65 |
|
66 |
// System.out.println("SEG_LINETO");
|
67 |
Coordinate to = new Coordinate(theData[0], theData[1]); |
68 |
LineSegment line = new LineSegment(from, to);
|
69 |
Coordinate closestPoint = line.closestPoint(c); |
70 |
double dist = c.distance(closestPoint);
|
71 |
|
72 |
if ((dist < minDist)) {
|
73 |
resul = new Point2D.Double(closestPoint.x, closestPoint.y); |
74 |
minDist = dist; |
75 |
} |
76 |
|
77 |
from = to; |
78 |
|
79 |
break;
|
80 |
|
81 |
case PathIterator.SEG_CLOSE: |
82 |
line = new LineSegment(from, first);
|
83 |
closestPoint = line.closestPoint(c); |
84 |
dist = c.distance(closestPoint); |
85 |
|
86 |
if ((dist < minDist)) {
|
87 |
resul = new Point2D.Double(closestPoint.x, closestPoint.y); |
88 |
minDist = dist; |
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, "tangent_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 |
g.drawLine((int) (pPixels.getX() - half),
|
117 |
(int) (pPixels.getY() - half), (int) (pPixels.getX() + half), |
118 |
(int) (pPixels.getY() - half));
|
119 |
g.drawOval((int) (pPixels.getX() - half),
|
120 |
(int) (pPixels.getY() - half), getSizePixels(), getSizePixels());
|
121 |
} |
122 |
|
123 |
/* (non-Javadoc)
|
124 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getPriority()
|
125 |
*/
|
126 |
public int getPriority() { |
127 |
return 10; |
128 |
} |
129 |
} |