svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / view / snapping / snappers / MediumPointSnapper.java @ 10626
History | View | Annotate | Download (3.92 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 |
|
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, IGeometry geom, |
31 |
double tolerance, Point2D lastPointEntered) { |
32 |
if (geom.getInternalShape() instanceof FCircle2D || |
33 |
geom.getInternalShape() instanceof FArc2D ||
|
34 |
geom.getInternalShape() instanceof FEllipse2D ||
|
35 |
geom.getInternalShape() instanceof FSpline2D) {
|
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 |
FConverter.FLATNESS); //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 PluginServices.getText(this, "medium_point"); |
102 |
} |
103 |
|
104 |
/* (non-Javadoc)
|
105 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapper#draw(java.awt.Graphics, java.awt.geom.Point2D)
|
106 |
*/
|
107 |
public void draw(Graphics g, Point2D pPixels) { |
108 |
g.setColor(getColor()); |
109 |
|
110 |
int half = getSizePixels() / 2; |
111 |
int x1 = (int) (pPixels.getX() - half); |
112 |
int x2 = (int) (pPixels.getX() + half); |
113 |
int x3 = (int) pPixels.getX(); |
114 |
int y1 = (int) (pPixels.getY() - half); |
115 |
int y2 = (int) (pPixels.getY() + half); |
116 |
|
117 |
g.drawLine(x1, y2, x2, y2); |
118 |
g.drawLine(x1, y2, x3, y1); |
119 |
g.drawLine(x2, y2, x3, y1); |
120 |
} |
121 |
|
122 |
} |