root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / view / snapping / snappers / MediumPointSnapper.java @ 8943
History | View | Annotate | Download (4.07 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 |
|
16 |
import java.awt.Graphics; |
17 |
import java.awt.geom.PathIterator; |
18 |
import java.awt.geom.Point2D; |
19 |
|
20 |
|
21 |
/**
|
22 |
* Medium point snapper.
|
23 |
*
|
24 |
* @author Vicente Caballero Navarro
|
25 |
*/
|
26 |
public class MediumPointSnapper extends AbstractSnapper |
27 |
implements ISnapperVectorial {
|
28 |
|
29 |
/* (non-Javadoc)
|
30 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getSnapPoint(Point2D point,
|
31 |
* IGeometry geom,double tolerance, Point2D lastPointEntered)
|
32 |
*/
|
33 |
public Point2D getSnapPoint(Point2D point, IGeometry geom, |
34 |
double tolerance, Point2D lastPointEntered) { |
35 |
if (geom.getInternalShape() instanceof FCircle2D || |
36 |
geom.getInternalShape() instanceof FArc2D ||
|
37 |
geom.getInternalShape() instanceof FEllipse2D ||
|
38 |
geom.getInternalShape() instanceof FSpline2D) {
|
39 |
return null; |
40 |
} |
41 |
|
42 |
Point2D resul = null; |
43 |
Coordinate c = new Coordinate(point.getX(), point.getY());
|
44 |
|
45 |
PathIterator theIterator = geom.getPathIterator(null, |
46 |
FConverter.FLATNESS); //polyLine.getPathIterator(null, flatness);
|
47 |
double[] theData = new double[6]; |
48 |
double minDist = tolerance;
|
49 |
Coordinate from = null;
|
50 |
Coordinate first = null;
|
51 |
|
52 |
while (!theIterator.isDone()) {
|
53 |
//while not done
|
54 |
int theType = theIterator.currentSegment(theData);
|
55 |
|
56 |
switch (theType) {
|
57 |
case PathIterator.SEG_MOVETO: |
58 |
from = new Coordinate(theData[0], theData[1]); |
59 |
first = from; |
60 |
|
61 |
break;
|
62 |
|
63 |
case PathIterator.SEG_LINETO: |
64 |
|
65 |
Coordinate to = new Coordinate(theData[0], theData[1]); |
66 |
Coordinate mediumPoint = new Coordinate((to.x + from.x) / 2, |
67 |
(to.y + from.y) / 2);
|
68 |
double dist = c.distance(mediumPoint);
|
69 |
|
70 |
if ((dist < minDist)) {
|
71 |
resul = new Point2D.Double(mediumPoint.x, mediumPoint.y); |
72 |
minDist = dist; |
73 |
} |
74 |
|
75 |
from = to; |
76 |
|
77 |
break;
|
78 |
|
79 |
case PathIterator.SEG_CLOSE: |
80 |
mediumPoint = new Coordinate((first.x + from.x) / 2, |
81 |
(first.y + from.y) / 2);
|
82 |
dist = c.distance(mediumPoint); |
83 |
|
84 |
if ((dist < minDist)) {
|
85 |
resul = new Point2D.Double(mediumPoint.x, mediumPoint.y); |
86 |
minDist = dist; |
87 |
} |
88 |
|
89 |
from = first; |
90 |
|
91 |
break;
|
92 |
} //end switch
|
93 |
|
94 |
theIterator.next(); |
95 |
} |
96 |
|
97 |
return resul;
|
98 |
} |
99 |
|
100 |
/* (non-Javadoc)
|
101 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getToolTipText()
|
102 |
*/
|
103 |
public String getToolTipText() { |
104 |
return PluginServices.getText(this, "medium_point"); |
105 |
} |
106 |
|
107 |
/* (non-Javadoc)
|
108 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#draw(java.awt.Graphics, java.awt.geom.Point2D)
|
109 |
*/
|
110 |
public void draw(Graphics g, Point2D pPixels) { |
111 |
g.setColor(getColor()); |
112 |
|
113 |
int half = getSizePixels() / 2; |
114 |
int x1 = (int) (pPixels.getX() - half); |
115 |
int x2 = (int) (pPixels.getX() + half); |
116 |
int x3 = (int) pPixels.getX(); |
117 |
int y1 = (int) (pPixels.getY() - half); |
118 |
int y2 = (int) (pPixels.getY() + half); |
119 |
|
120 |
g.drawLine(x1, y2, x2, y2); |
121 |
g.drawLine(x1, y2, x3, y1); |
122 |
g.drawLine(x2, y2, x3, y1); |
123 |
} |
124 |
|
125 |
/* (non-Javadoc)
|
126 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#getPriority()
|
127 |
*/
|
128 |
public int getPriority() { |
129 |
return 8; |
130 |
} |
131 |
} |