root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / view / snapping / snappers / NearestPointSnapper.java @ 8943
History | View | Annotate | Download (2.89 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 |
public class NearestPointSnapper extends AbstractSnapper implements ISnapperVectorial { |
16 |
|
17 |
public Point2D getSnapPoint(Point2D point, IGeometry geom, double tolerance, Point2D lastPointEntered) { |
18 |
Point2D resul = null; |
19 |
Coordinate c = new Coordinate(point.getX(), point.getY());
|
20 |
|
21 |
PathIterator theIterator = geom.getPathIterator(null, FConverter.FLATNESS); //polyLine.getPathIterator(null, flatness); |
22 |
double[] theData = new double[6]; |
23 |
double minDist = tolerance;
|
24 |
Coordinate from = null, first = null; |
25 |
while (!theIterator.isDone()) {
|
26 |
//while not done
|
27 |
int theType = theIterator.currentSegment(theData);
|
28 |
|
29 |
switch (theType) {
|
30 |
case PathIterator.SEG_MOVETO: |
31 |
from = new Coordinate(theData[0], theData[1]); |
32 |
first = from; |
33 |
break;
|
34 |
|
35 |
case PathIterator.SEG_LINETO: |
36 |
|
37 |
// System.out.println("SEG_LINETO");
|
38 |
Coordinate to = new Coordinate(theData[0], theData[1]); |
39 |
LineSegment line = new LineSegment(from, to);
|
40 |
Coordinate closestPoint = line.closestPoint(c); |
41 |
double dist = c.distance(closestPoint);
|
42 |
if ((dist < minDist)) {
|
43 |
resul = new Point2D.Double(closestPoint.x, closestPoint.y); |
44 |
minDist = dist; |
45 |
} |
46 |
|
47 |
from = to; |
48 |
break;
|
49 |
case PathIterator.SEG_CLOSE: |
50 |
line = new LineSegment(from, first);
|
51 |
closestPoint = line.closestPoint(c); |
52 |
dist = c.distance(closestPoint); |
53 |
if ((dist < minDist)) {
|
54 |
resul = new Point2D.Double(closestPoint.x, closestPoint.y); |
55 |
minDist = dist; |
56 |
} |
57 |
|
58 |
from = first; |
59 |
break;
|
60 |
|
61 |
|
62 |
} //end switch
|
63 |
|
64 |
theIterator.next(); |
65 |
} |
66 |
|
67 |
return resul;
|
68 |
} |
69 |
|
70 |
public String getToolTipText() { |
71 |
return PluginServices.getText(this, "nearest_point"); |
72 |
} |
73 |
|
74 |
/* (non-Javadoc)
|
75 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#draw(java.awt.Graphics, java.awt.geom.Point2D)
|
76 |
*/
|
77 |
public void draw(Graphics g, Point2D pPixels) { |
78 |
g.setColor(getColor()); |
79 |
int half = getSizePixels() / 2; |
80 |
int x1 = (int) (pPixels.getX() - half); |
81 |
int x2 = (int) (pPixels.getX() + half); |
82 |
int y1 = (int) (pPixels.getY() - half); |
83 |
int y2 = (int) (pPixels.getY() + half); |
84 |
|
85 |
g.drawLine(x1, y1, x2, y1); // abajo
|
86 |
g.drawLine(x1, y2, x2, y2); // arriba
|
87 |
g.drawLine(x1, y1, x2, y2); // abajo - arriba
|
88 |
g.drawLine(x1, y2, x2, y1); // arriba - abajo
|
89 |
} |
90 |
|
91 |
/* (non-Javadoc)
|
92 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getPriority()
|
93 |
*/
|
94 |
public int getPriority() |
95 |
{ |
96 |
return 0; |
97 |
} |
98 |
|
99 |
} |