Statistics
| Revision:

root / org.gvsig.dxf / trunk / org.gvsig.dxf / org.gvsig.dxf.lib / src / main / java / org / gvsig / dxf / px / dxf / DxfCalArcs.java @ 6

History | View | Annotate | Download (6.41 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.gvsig.dxf.px.dxf;
25

    
26
import java.awt.geom.Point2D;
27
import java.util.Vector;
28

    
29

    
30
/**
31
 * Calcula puntos sobre un arco. Originalmente implementada para calcular los puntos
32
 * con un espaciamiento de un grado sexagesimal. Para el c�lculo de los puntos del arco
33
 * utiliza el primer y el �tlimo punto del arco y un par�metro de curvatura.
34
 * @author jmorell
35
 *
36
 */
37
public class DxfCalArcs {
38
    final boolean debug = true;
39
    Point2D coord1;
40
    Point2D coord2;
41
    Point2D center;
42
    double radio;
43
    double empieza;
44
    double acaba;
45
    double bulge;
46
    double d;
47
    double dd;
48
    double aci;
49
    Point2D coordAux;
50

    
51
    //private Point2D centralPoint;
52
    
53
    /**
54
     * Constructor de DxfCalArcs.
55
     * @param p1, punto inicial del arco.
56
     * @param p2, punto final del arco.
57
     * @param bulge, par�metro de curvatura.
58
     */
59
    public DxfCalArcs(Point2D p1, Point2D p2, double bulge) {
60
        this.bulge = bulge;
61

    
62
        if (bulge < 0.0) {
63
            coord1 = p2;
64
            coord2 = p1;
65

    
66
        } else {
67
            coord1 = p1;
68
            coord2 = p2;
69
        }
70

    
71
        calculate();
72
    }
73
    
74
    /**
75
     * Calcula puntos sobre un arco.
76
     * @return DxfCalArcs
77
     */
78
    DxfCalArcs calculate() {
79
        d = Math.sqrt(((coord2.getX() - coord1.getX()) * (coord2.getX() -
80
                      coord1.getX())) +
81
                      ((coord2.getY() - coord1.getY()) * (coord2.getY() -
82
                      coord1.getY())));
83

    
84
        coordAux = new Point2D.Double((coord1.getX() + coord2.getX()) / 2.0,
85
                                      (coord1.getY() + coord2.getY()) / 2.0);
86

    
87
        double b = Math.abs(bulge);
88

    
89
        double beta = Math.atan(b);
90

    
91
        double alfa = beta * 4.0;
92

    
93
        double landa = alfa / 2.0;
94

    
95
        dd = (d / 2.0) / (Math.tan(landa));
96
        radio = (d / 2.0) / (Math.sin(landa));
97

    
98
        aci = Math.atan((coord2.getX() - coord1.getX()) / (coord2.getY() -
99
                        coord1.getY()));
100

    
101
        double aciDegree = (aci * 180.0) / Math.PI;
102

    
103
        if (coord2.getY() > coord1.getY()) {
104
            aci += Math.PI;
105

    
106
            aciDegree = (aci * 180.0) / Math.PI;
107

    
108
        }
109

    
110
        center = new Point2D.Double(coordAux.getX() +
111
                                    (dd * Math.sin(aci + (Math.PI / 2.0))),
112
                                    coordAux.getY() +
113
                                    (dd * Math.cos(aci + (Math.PI / 2.0))));
114

    
115
        calculateEA(alfa);
116

    
117
        return this;
118
    }
119
    
120
    /**
121
     * Calcula el �ngulo de comienzo y el de finalizaci�n del arco.
122
     * @param alfa, �ngulo del arco. 
123
     */
124
    void calculateEA(double alfa) {
125
        empieza = Math.atan2(coord1.getY() - center.getY(),
126
                             coord1.getX() - center.getX());
127
        acaba = (empieza + alfa);
128
        empieza = (empieza * 180.0) / Math.PI;
129
        acaba = (acaba * 180.0) / Math.PI;
130

    
131
    }
132
    
133
    /**
134
     * Devuelve un Vector con los puntos que conforman el arco.
135
     * @param inc, es el espaciamiento entre puntos (un grado sexagesimal por defecto).
136
     * @return Vector de puntos.
137
     */
138
    public Vector getPoints(double inc) {
139
        Vector arc = new Vector();
140
        double angulo;
141

    
142
        int iempieza = (int) empieza + 1; // ojo aqui !!
143
        int iacaba = (int) acaba;
144

    
145
        if (empieza <= acaba) {
146
            addNode(arc, empieza);
147

    
148
            for (angulo = iempieza; angulo <= iacaba; angulo += inc) {
149
                addNode(arc, angulo);
150
            }
151

    
152
            addNode(arc, acaba);
153
        } else {
154
            addNode(arc, empieza);
155

    
156
            for (angulo = iempieza; angulo <= 360; angulo += inc) {
157
                addNode(arc, angulo);
158
            }
159

    
160
            for (angulo = 1; angulo <= iacaba; angulo += inc) {
161
                addNode(arc, angulo);
162
            }
163

    
164
            addNode(arc, angulo);
165
        }
166

    
167
        Point2D aux = (Point2D) arc.get(arc.size() - 1);
168
        double aux1 = Math.abs(aux.getX() - coord2.getX());
169
        double aux2 = Math.abs(aux.getY() - coord2.getY());
170

    
171
        /*if (aux1<=0.000005 && aux2<=0.000005) {
172
                arc.remove(arc.size()-1);
173
                arc.remove(arc.size()-1);
174
                arc.add(coord2);
175
        }*/
176
        return arc;
177
    }
178

    
179
    /**
180
     * Devuelve un Vector con puntos pertenecientes al arco y situados cerca de su
181
     * punto central.
182
     * @return Vector
183
     */
184
    public Vector getCentralPoint() {
185
        Vector arc = new Vector();
186

    
187
        if (empieza <= acaba) {
188
            addNode(arc, (empieza + acaba) / 2.0);
189
        } else {
190
            addNode(arc, empieza);
191

    
192
            double alfa = 360 - empieza;
193
            double beta = acaba;
194
            double an = alfa + beta;
195
            double mid = an / 2.0;
196

    
197
            if (mid <= alfa) {
198
                addNode(arc, empieza + mid);
199
            } else {
200
                addNode(arc, mid - alfa);
201
            }
202
        }
203

    
204
        return arc;
205
    }
206
    
207
    /**
208
     * A�ade un punto al arco basandose en un �ngulo dado.
209
     * @param arc, arco al que se a�ade el punto.
210
     * @param angulo, �ngulo donde se localiza el punto.
211
     */
212
    private void addNode(Vector arc, double angulo) {
213
        double yy = center.getY() +
214
                    (radio * Math.sin((angulo * Math.PI) / 180.0));
215
        double xx = center.getX() +
216
                    (radio * Math.cos((angulo * Math.PI) / 180.0));
217
        arc.add(new Point2D.Double(xx, yy));
218

    
219
    }
220
}