root / tags / v2_0_0_Build_2049 / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / snapping / snappers / MediumPointSnapper.java @ 38460
History | View | Annotate | Download (4.03 KB)
1 |
package org.gvsig.app.project.documents.view.toolListeners.snapping.snappers; |
---|---|
2 |
|
3 |
import java.awt.geom.PathIterator; |
4 |
import java.awt.geom.Point2D; |
5 |
|
6 |
import org.gvsig.fmap.geom.Geometry; |
7 |
import org.gvsig.fmap.geom.primitive.Arc; |
8 |
import org.gvsig.fmap.geom.primitive.Circle; |
9 |
import org.gvsig.fmap.geom.primitive.Ellipse; |
10 |
import org.gvsig.fmap.geom.primitive.Spline; |
11 |
import org.gvsig.fmap.mapcontrol.PrimitivesDrawer; |
12 |
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperVectorial; |
13 |
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.impl.AbstractSnapper; |
14 |
import org.gvsig.i18n.Messages; |
15 |
|
16 |
import com.vividsolutions.jts.geom.Coordinate; |
17 |
|
18 |
|
19 |
/**
|
20 |
* Medium point snapper.
|
21 |
*
|
22 |
* @author Vicente Caballero Navarro
|
23 |
*/
|
24 |
public class MediumPointSnapper extends AbstractSnapper |
25 |
implements ISnapperVectorial {
|
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, Geometry geom, |
31 |
double tolerance, Point2D lastPointEntered) { |
32 |
if (geom instanceof Circle || |
33 |
geom instanceof Arc ||
|
34 |
geom instanceof Ellipse ||
|
35 |
geom instanceof Spline) {
|
36 |
return null; |
37 |
} |
38 |
|
39 |
Point2D resul = null; |
40 |
Coordinate c = new Coordinate(point.getX(), point.getY());
|
41 |
|
42 |
PathIterator theIterator = geom.getPathIterator(null, |
43 |
geomManager.getFlatness()); //polyLine.getPathIterator(null, flatness);
|
44 |
double[] theData = new double[6]; |
45 |
double minDist = tolerance;
|
46 |
Coordinate from = null;
|
47 |
Coordinate first = null;
|
48 |
|
49 |
while (!theIterator.isDone()) {
|
50 |
//while not done
|
51 |
int theType = theIterator.currentSegment(theData);
|
52 |
|
53 |
switch (theType) {
|
54 |
case PathIterator.SEG_MOVETO: |
55 |
from = new Coordinate(theData[0], theData[1]); |
56 |
first = from; |
57 |
|
58 |
break;
|
59 |
|
60 |
case PathIterator.SEG_LINETO: |
61 |
|
62 |
Coordinate to = new Coordinate(theData[0], theData[1]); |
63 |
Coordinate mediumPoint = new Coordinate((to.x + from.x) / 2, |
64 |
(to.y + from.y) / 2);
|
65 |
double dist = c.distance(mediumPoint);
|
66 |
|
67 |
if ((dist < minDist)) {
|
68 |
resul = new Point2D.Double(mediumPoint.x, mediumPoint.y); |
69 |
minDist = dist; |
70 |
} |
71 |
|
72 |
from = to; |
73 |
|
74 |
break;
|
75 |
|
76 |
case PathIterator.SEG_CLOSE: |
77 |
mediumPoint = new Coordinate((first.x + from.x) / 2, |
78 |
(first.y + from.y) / 2);
|
79 |
dist = c.distance(mediumPoint); |
80 |
|
81 |
if ((dist < minDist)) {
|
82 |
resul = new Point2D.Double(mediumPoint.x, mediumPoint.y); |
83 |
minDist = dist; |
84 |
} |
85 |
|
86 |
from = first; |
87 |
|
88 |
break;
|
89 |
} //end switch
|
90 |
|
91 |
theIterator.next(); |
92 |
} |
93 |
|
94 |
return resul;
|
95 |
} |
96 |
|
97 |
/* (non-Javadoc)
|
98 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getToolTipText()
|
99 |
*/
|
100 |
public String getToolTipText() { |
101 |
return Messages.getText("Medium_point"); |
102 |
} |
103 |
|
104 |
/*
|
105 |
* (non-Javadoc)
|
106 |
* @see org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper#draw(org.gvsig.fmap.mapcontrol.PrimitivesDrawer, java.awt.geom.Point2D)
|
107 |
*/
|
108 |
public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) { |
109 |
primitivesDrawer.setColor(getColor()); |
110 |
|
111 |
int half = getSizePixels() / 2; |
112 |
int x1 = (int) (pPixels.getX() - half); |
113 |
int x2 = (int) (pPixels.getX() + half); |
114 |
int x3 = (int) pPixels.getX(); |
115 |
int y1 = (int) (pPixels.getY() - half); |
116 |
int y2 = (int) (pPixels.getY() + half); |
117 |
|
118 |
primitivesDrawer.drawLine(x1, y2, x2, y2); |
119 |
primitivesDrawer.drawLine(x1, y2, x3, y1); |
120 |
primitivesDrawer.drawLine(x2, y2, x3, y1); |
121 |
} |
122 |
|
123 |
} |