svn-gvsig-desktop / branches / v02_desarrollo / libraries / libCq CMS for java.old / src / org / cresques / px / dxf / DxfCalArcs.java @ 1607
History | View | Annotate | Download (5.33 KB)
1 |
package org.cresques.px.dxf; |
---|---|
2 |
|
3 |
import java.awt.geom.Point2D; |
4 |
import java.util.Vector; |
5 |
|
6 |
/**
|
7 |
* Calcula puntos sobre un arco.
|
8 |
* @author jmorell
|
9 |
*
|
10 |
* TODO To change the template for this generated type comment go to
|
11 |
* Window - Preferences - Java - Code Style - Code Templates
|
12 |
*/
|
13 |
public class DxfCalArcs { |
14 |
final boolean debug = true; |
15 |
|
16 |
Point2D coord1, coord2;
|
17 |
Point2D center;
|
18 |
double radio, empieza, acaba;
|
19 |
double bulge;
|
20 |
double d, dd, aci;
|
21 |
Point2D coordAux;
|
22 |
//private Point2D centralPoint;
|
23 |
|
24 |
public DxfCalArcs(Point2D p1, Point2D p2, double bulge) { |
25 |
this.bulge = bulge;
|
26 |
/*System.out.println("DxfCalArcs: p1 = " + p1);
|
27 |
System.out.println("DxfCalArcs: p2 = " + p2);
|
28 |
System.out.println("DxfCalArcs: bulge = " + bulge);*/
|
29 |
if (bulge < 0.0) { |
30 |
coord1 = p2; |
31 |
coord2 = p1; |
32 |
//System.out.println("DxfCalArcs: Bulge negativo; coord1 = " + coord1 + ", coord2 = " + coord2);
|
33 |
} else {
|
34 |
coord1 = p1; |
35 |
coord2 = p2; |
36 |
} |
37 |
calculate(); |
38 |
} |
39 |
|
40 |
DxfCalArcs calculate() { |
41 |
d = Math.sqrt((coord2.getX()-coord1.getX())*(coord2.getX()-coord1.getX()) + (coord2.getY()-coord1.getY())*(coord2.getY()-coord1.getY()));
|
42 |
//System.out.println("DxfCalArcs: distancia reducida = " + d);
|
43 |
|
44 |
coordAux = new Point2D.Double((coord1.getX()+coord2.getX())/2.0, (coord1.getY()+coord2.getY())/2.0); |
45 |
//System.out.println("DxfCalArcs: punto medio = " + coordAux);
|
46 |
|
47 |
double b = Math.abs(bulge); |
48 |
//System.out.println("DxfCalArcs: Bulge(valor absoluto) = " + b);
|
49 |
double beta = Math.atan(b); |
50 |
//System.out.println("DxfCalArcs: Angulo beta(rad) = " + beta);
|
51 |
//double beta = Math.atan(bulge);
|
52 |
double alfa = beta*4.0; |
53 |
//System.out.println("DxfCalArcs: Angulo alfa(rad)(angulo que define el arco) = " + alfa);
|
54 |
double landa = alfa/2.0; |
55 |
//System.out.println("DxfCalArcs: Angulo landa(rad) = " + landa);
|
56 |
dd = (d/2.0)/(Math.tan(landa)); |
57 |
radio = (d/2.0)/(Math.sin(landa)); |
58 |
//System.out.println("DxfCalArcs: radio del arco = " + radio);
|
59 |
aci = Math.atan((coord2.getX()-coord1.getX())/(coord2.getY()-coord1.getY()));
|
60 |
//System.out.println("DxfCalArcs: Acimut de coord1(pto origen) a coord2(pto final) = " + aci);
|
61 |
double aciDegree = aci*180.0/Math.PI; |
62 |
//System.out.println("DxfCalArcs: Acimut en grados = " + aciDegree);
|
63 |
if (coord2.getY() > coord1.getY()) {
|
64 |
aci += Math.PI;
|
65 |
//System.out.println("DxfCalArcs: La coord Y de coord2 es mayor que la");
|
66 |
//System.out.println("DxfCalArcs: coord Y de coord1, acimut = acimut + PI, nuevo acimut = " + aci);
|
67 |
aciDegree = aci*180.0/Math.PI; |
68 |
//System.out.println("DxfCalArcs: Nuevo acimut en grados = " + aciDegree);
|
69 |
} |
70 |
center = new Point2D.Double(coordAux.getX() + dd*Math.sin(aci+(Math.PI/2.0)), coordAux.getY() + dd*Math.cos(aci+(Math.PI/2.0))); |
71 |
//System.out.println("DxfCalArcs: El centro del arco es: centro = " + center);
|
72 |
calculateEA(alfa); |
73 |
return this; |
74 |
} |
75 |
|
76 |
void calculateEA(double alfa){ |
77 |
empieza = Math.atan2(coord1.getY()-center.getY(), coord1.getX()-center.getX());
|
78 |
acaba = (empieza + alfa); |
79 |
empieza = empieza*180.0/Math.PI; |
80 |
acaba = acaba*180.0/Math.PI; |
81 |
//System.out.println("DxfCalArcs: Acimut de inicio = " + empieza + ", Acimut de finalizacion = " + acaba);
|
82 |
//System.out.println("Tener en cuenta que los angulos se miden en sentido antihorario.");
|
83 |
} |
84 |
|
85 |
public Vector getPoints(double inc) { |
86 |
//System.out.println("Se incia el metodo que construye el arco.");
|
87 |
//System.out.println("Todo el proceso anterior define los parametros para esta construccion.");
|
88 |
Vector arc = new Vector(); |
89 |
double angulo;
|
90 |
|
91 |
int iempieza = (int) empieza + 1; // ojo aqui !! |
92 |
int iacaba = (int) acaba; |
93 |
//System.out.println("DxfCalArcs: Angulo entero de inicio: iempieza = " + iempieza + ", Angulo entero de finalizacion: iacaba = " + iacaba);
|
94 |
|
95 |
if (empieza <= acaba) {
|
96 |
//System.out.println("El angulo de inicio es menor que el angulo de finalizacion.");
|
97 |
addNode(arc, empieza); |
98 |
for (angulo = iempieza; angulo <= iacaba; angulo += inc) {
|
99 |
addNode(arc, angulo); |
100 |
} |
101 |
addNode(arc, acaba); |
102 |
} else {
|
103 |
//System.out.println("El angulo de inicio es mayor que el angulo de finalizacion.");
|
104 |
addNode(arc, empieza); |
105 |
for (angulo = iempieza ; angulo <= 360; angulo += inc) { |
106 |
addNode(arc, angulo); |
107 |
} |
108 |
for (angulo = 1; angulo <= iacaba; angulo += inc) { |
109 |
addNode(arc, angulo); |
110 |
} |
111 |
addNode(arc, angulo); |
112 |
} |
113 |
Point2D aux = (Point2D)arc.get(arc.size()-1); |
114 |
double aux1 = Math.abs(aux.getX()-coord2.getX()); |
115 |
double aux2 = Math.abs(aux.getY()-coord2.getY()); |
116 |
/*if (aux1<=0.000005 && aux2<=0.000005) {
|
117 |
arc.remove(arc.size()-1);
|
118 |
arc.remove(arc.size()-1);
|
119 |
arc.add(coord2);
|
120 |
}*/
|
121 |
return arc;
|
122 |
} |
123 |
|
124 |
/**
|
125 |
* 050301, jmorell: M?todo para obtener el punto central del arco.
|
126 |
* @return
|
127 |
*/
|
128 |
public Vector getCentralPoint() { |
129 |
Vector arc = new Vector(); |
130 |
if (empieza <= acaba) {
|
131 |
addNode(arc, (empieza+acaba)/2.0);
|
132 |
} else {
|
133 |
addNode(arc, empieza); |
134 |
double alfa = 360-empieza; |
135 |
double beta = acaba;
|
136 |
double an = alfa + beta;
|
137 |
double mid = an/2.0; |
138 |
if (mid<=alfa) {
|
139 |
addNode(arc, empieza+mid); |
140 |
} else {
|
141 |
addNode(arc, mid-alfa); |
142 |
} |
143 |
} |
144 |
return arc;
|
145 |
} |
146 |
|
147 |
private void addNode(Vector arc, double angulo) { |
148 |
double yy = center.getY() + radio * Math.sin(angulo*Math.PI/180.0); |
149 |
double xx = center.getX() + radio * Math.cos(angulo*Math.PI/180.0); |
150 |
arc.add(new Point2D.Double(xx,yy)); |
151 |
//System.out.println("DxfCalArcs: A?ade el punto " + new Point2D.Double(xx,yy) + ", correspondiente al angulo " + angulo);
|
152 |
} |
153 |
|
154 |
} |