svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / px / dxf / DxfCalArcs.java @ 2312
History | View | Annotate | Download (6.08 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.px.dxf; |
25 |
|
26 |
import java.awt.geom.Point2D; |
27 |
import java.util.Vector; |
28 |
|
29 |
/**
|
30 |
* Calcula puntos sobre un arco.
|
31 |
* @author jmorell
|
32 |
*
|
33 |
*/
|
34 |
public class DxfCalArcs { |
35 |
final boolean debug = true; |
36 |
|
37 |
Point2D coord1, coord2;
|
38 |
Point2D center;
|
39 |
double radio, empieza, acaba;
|
40 |
double bulge;
|
41 |
double d, dd, aci;
|
42 |
Point2D coordAux;
|
43 |
//private Point2D centralPoint;
|
44 |
|
45 |
public DxfCalArcs(Point2D p1, Point2D p2, double bulge) { |
46 |
this.bulge = bulge;
|
47 |
/*System.out.println("DxfCalArcs: p1 = " + p1);
|
48 |
System.out.println("DxfCalArcs: p2 = " + p2);
|
49 |
System.out.println("DxfCalArcs: bulge = " + bulge);*/
|
50 |
if (bulge < 0.0) { |
51 |
coord1 = p2; |
52 |
coord2 = p1; |
53 |
//System.out.println("DxfCalArcs: Bulge negativo; coord1 = " + coord1 + ", coord2 = " + coord2);
|
54 |
} else {
|
55 |
coord1 = p1; |
56 |
coord2 = p2; |
57 |
} |
58 |
calculate(); |
59 |
} |
60 |
|
61 |
DxfCalArcs calculate() { |
62 |
d = Math.sqrt((coord2.getX()-coord1.getX())*(coord2.getX()-coord1.getX()) + (coord2.getY()-coord1.getY())*(coord2.getY()-coord1.getY()));
|
63 |
//System.out.println("DxfCalArcs: distancia reducida = " + d);
|
64 |
|
65 |
coordAux = new Point2D.Double((coord1.getX()+coord2.getX())/2.0, (coord1.getY()+coord2.getY())/2.0); |
66 |
//System.out.println("DxfCalArcs: punto medio = " + coordAux);
|
67 |
|
68 |
double b = Math.abs(bulge); |
69 |
//System.out.println("DxfCalArcs: Bulge(valor absoluto) = " + b);
|
70 |
double beta = Math.atan(b); |
71 |
//System.out.println("DxfCalArcs: Angulo beta(rad) = " + beta);
|
72 |
//double beta = Math.atan(bulge);
|
73 |
double alfa = beta*4.0; |
74 |
//System.out.println("DxfCalArcs: Angulo alfa(rad)(angulo que define el arco) = " + alfa);
|
75 |
double landa = alfa/2.0; |
76 |
//System.out.println("DxfCalArcs: Angulo landa(rad) = " + landa);
|
77 |
dd = (d/2.0)/(Math.tan(landa)); |
78 |
radio = (d/2.0)/(Math.sin(landa)); |
79 |
//System.out.println("DxfCalArcs: radio del arco = " + radio);
|
80 |
aci = Math.atan((coord2.getX()-coord1.getX())/(coord2.getY()-coord1.getY()));
|
81 |
//System.out.println("DxfCalArcs: Acimut de coord1(pto origen) a coord2(pto final) = " + aci);
|
82 |
double aciDegree = aci*180.0/Math.PI; |
83 |
//System.out.println("DxfCalArcs: Acimut en grados = " + aciDegree);
|
84 |
if (coord2.getY() > coord1.getY()) {
|
85 |
aci += Math.PI;
|
86 |
//System.out.println("DxfCalArcs: La coord Y de coord2 es mayor que la");
|
87 |
//System.out.println("DxfCalArcs: coord Y de coord1, acimut = acimut + PI, nuevo acimut = " + aci);
|
88 |
aciDegree = aci*180.0/Math.PI; |
89 |
//System.out.println("DxfCalArcs: Nuevo acimut en grados = " + aciDegree);
|
90 |
} |
91 |
center = new Point2D.Double(coordAux.getX() + dd*Math.sin(aci+(Math.PI/2.0)), coordAux.getY() + dd*Math.cos(aci+(Math.PI/2.0))); |
92 |
//System.out.println("DxfCalArcs: El centro del arco es: centro = " + center);
|
93 |
calculateEA(alfa); |
94 |
return this; |
95 |
} |
96 |
|
97 |
void calculateEA(double alfa){ |
98 |
empieza = Math.atan2(coord1.getY()-center.getY(), coord1.getX()-center.getX());
|
99 |
acaba = (empieza + alfa); |
100 |
empieza = empieza*180.0/Math.PI; |
101 |
acaba = acaba*180.0/Math.PI; |
102 |
//System.out.println("DxfCalArcs: Acimut de inicio = " + empieza + ", Acimut de finalizacion = " + acaba);
|
103 |
//System.out.println("Tener en cuenta que los angulos se miden en sentido antihorario.");
|
104 |
} |
105 |
|
106 |
public Vector getPoints(double inc) { |
107 |
//System.out.println("Se incia el metodo que construye el arco.");
|
108 |
//System.out.println("Todo el proceso anterior define los parametros para esta construccion.");
|
109 |
Vector arc = new Vector(); |
110 |
double angulo;
|
111 |
|
112 |
int iempieza = (int) empieza + 1; // ojo aqui !! |
113 |
int iacaba = (int) acaba; |
114 |
//System.out.println("DxfCalArcs: Angulo entero de inicio: iempieza = " + iempieza + ", Angulo entero de finalizacion: iacaba = " + iacaba);
|
115 |
|
116 |
if (empieza <= acaba) {
|
117 |
//System.out.println("El angulo de inicio es menor que el angulo de finalizacion.");
|
118 |
addNode(arc, empieza); |
119 |
for (angulo = iempieza; angulo <= iacaba; angulo += inc) {
|
120 |
addNode(arc, angulo); |
121 |
} |
122 |
addNode(arc, acaba); |
123 |
} else {
|
124 |
//System.out.println("El angulo de inicio es mayor que el angulo de finalizacion.");
|
125 |
addNode(arc, empieza); |
126 |
for (angulo = iempieza ; angulo <= 360; angulo += inc) { |
127 |
addNode(arc, angulo); |
128 |
} |
129 |
for (angulo = 1; angulo <= iacaba; angulo += inc) { |
130 |
addNode(arc, angulo); |
131 |
} |
132 |
addNode(arc, angulo); |
133 |
} |
134 |
Point2D aux = (Point2D)arc.get(arc.size()-1); |
135 |
double aux1 = Math.abs(aux.getX()-coord2.getX()); |
136 |
double aux2 = Math.abs(aux.getY()-coord2.getY()); |
137 |
/*if (aux1<=0.000005 && aux2<=0.000005) {
|
138 |
arc.remove(arc.size()-1);
|
139 |
arc.remove(arc.size()-1);
|
140 |
arc.add(coord2);
|
141 |
}*/
|
142 |
return arc;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* 050301, jmorell: M?todo para obtener el punto central del arco.
|
147 |
* @return
|
148 |
*/
|
149 |
public Vector getCentralPoint() { |
150 |
Vector arc = new Vector(); |
151 |
if (empieza <= acaba) {
|
152 |
addNode(arc, (empieza+acaba)/2.0);
|
153 |
} else {
|
154 |
addNode(arc, empieza); |
155 |
double alfa = 360-empieza; |
156 |
double beta = acaba;
|
157 |
double an = alfa + beta;
|
158 |
double mid = an/2.0; |
159 |
if (mid<=alfa) {
|
160 |
addNode(arc, empieza+mid); |
161 |
} else {
|
162 |
addNode(arc, mid-alfa); |
163 |
} |
164 |
} |
165 |
return arc;
|
166 |
} |
167 |
|
168 |
private void addNode(Vector arc, double angulo) { |
169 |
double yy = center.getY() + radio * Math.sin(angulo*Math.PI/180.0); |
170 |
double xx = center.getX() + radio * Math.cos(angulo*Math.PI/180.0); |
171 |
arc.add(new Point2D.Double(xx,yy)); |
172 |
//System.out.println("DxfCalArcs: A?ade el punto " + new Point2D.Double(xx,yy) + ", correspondiente al angulo " + angulo);
|
173 |
} |
174 |
|
175 |
} |