Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / util / ArcFromBulgeCalculator.java @ 12378

History | View | Annotate | Download (4.79 KB)

1
/* jdwglib. Java Library for reading Dwg files.
2
 * 
3
 * Author: Jose Morell Rama (jose.morell@gmail.com).
4
 * Port from the Pythoncad Dwg library by Art Haas.
5
 *
6
 * Copyright (C) 2005 Jose Morell, IVER TI S.A. and Generalitat Valenciana
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 * Jose Morell (jose.morell@gmail.com)
25
 * 
26
 * or
27
 *
28
 * IVER TI S.A.
29
 *  C/Salamanca, 50
30
 *  46005 Valencia
31
 *  Spain
32
 *  +34 963163400
33
 *  dac@iver.es
34
 */
35
package com.iver.cit.jdwglib.util;
36

    
37
import java.awt.geom.Point2D;
38
import java.util.Vector;
39

    
40
/**
41
 * This class calculates an arc given by a start and end points and a bulge
42
 * 
43
 * @author jmorell
44
 */
45
public class ArcFromBulgeCalculator {
46
        private double[] coord1, coord2;
47
        private double[] center;
48
        private double radio, empieza, acaba;
49
        private double bulge;
50
        private double d, dd, aci;
51
        private double[] coordAux;
52
        
53
        /**
54
         * This method calculates an arc given by a start and end points and a bulge
55
         * 
56
         * @param p1 Start point of the arc given by a Point2D
57
         * @param p2 End point of the arc given by a Point2D
58
         * @param bulge Bulge of the arc given by a double value 
59
         */
60
        public ArcFromBulgeCalculator(double[] p1, double[] p2, double bulge) {
61
                this.bulge = bulge;
62
                if (bulge < 0.0) {
63
                        coord1 = p2;
64
                        coord2 = p1;
65
                } else {
66
                        coord1 = p1;
67
                        coord2 = p2;
68
                }
69
                calParams();
70
        }
71
        
72
        private void calParams() {
73
                d = Math.sqrt((coord2[0]-coord1[0])*(coord2[0]-coord1[0]) + 
74
                                (coord2[1]-coord1[1])*(coord2[1]-coord1[1]));
75
                coordAux = new double[]{(coord1[0]+coord2[0])/2.0, (coord1[1]+coord2[1])/2.0};
76
                double b = Math.abs(bulge);
77
                double beta = Math.atan(b);
78
                double alfa = beta*4.0;
79
                double landa = alfa/2.0;
80
                dd = (d/2.0)/(Math.tan(landa));
81
                radio = (d/2.0)/(Math.sin(landa));                
82
                aci = Math.atan((coord2[0]-coord1[0])/(coord2[1]-coord1[1]));
83
                double aciDegree = aci*180.0/Math.PI;
84
                if (coord2[1] > coord1[1]) {
85
                        aci += Math.PI;
86
                        aciDegree = aci*180.0/Math.PI;
87
                }
88
                center = new double[]{coordAux[0] + dd*Math.sin(aci+(Math.PI/2.0)), coordAux[1] + dd*Math.cos(aci+(Math.PI/2.0))};
89
                calEA(alfa);
90
        }
91
        
92
        private void calEA(double alfa){
93
                empieza = Math.atan2(coord1[1]-center[1], coord1[0]-center[0]);
94
                acaba = (empieza + alfa);
95
                empieza = empieza*180.0/Math.PI;
96
                acaba = acaba*180.0/Math.PI;
97
        }
98
        
99
        /**
100
         * This method calculates an arc in a Gis geometry model. This arc is represented in
101
         * this model by a Vector of Point2D. The distance between points in the arc is given
102
         * as an argument
103
         * 
104
         * @param inc Distance between points in the arc
105
         * @return Vector Vector with the set of Point2D that represents the arc
106
         */
107
        public Vector<double[]> getPoints(double inc) {
108
                Vector<double[]> arc = new Vector<double[]>();
109
                double angulo;
110
                int iempieza = (int) empieza + 1;
111
                int iacaba = (int) acaba;
112
                if (empieza <= acaba) {
113
                        addNode(arc, empieza);
114
                        for (angulo = iempieza; angulo <= iacaba; angulo += inc) {
115
                                addNode(arc, angulo);
116
                        }
117
                        addNode(arc, acaba);
118
                } else {
119
                        addNode(arc, empieza);
120
                        for (angulo = iempieza ; angulo <= 360; angulo += inc) {
121
                                addNode(arc, angulo);
122
                        }
123
                        for (angulo = 1; angulo <= iacaba; angulo += inc) {
124
                                addNode(arc, angulo);
125
                        }
126
                        addNode(arc, angulo);
127
                }
128
//                Point2D aux = (Point2D)arc.get(arc.size()-1);
129
                double[] aux = arc.get(arc.size() -1);
130
                double aux1 = Math.abs(aux[0]-coord2[0]);
131
                double aux2 = Math.abs(aux[1]-coord2[1]);
132
                return arc;
133
        }
134
        
135
        /**
136
         * Method that allows to obtain a set of points located in the central zone of 
137
         * this arc object
138
         */
139
        public Vector<double[]> getCentralPoint() {
140
                Vector<double[]> arc = new Vector<double[]>();
141
                if (empieza <= acaba) {
142
                        addNode(arc, (empieza+acaba)/2.0);
143
                } else {
144
                        addNode(arc, empieza);
145
                        double alfa = 360-empieza;
146
                        double beta = acaba;
147
                        double an = alfa + beta;
148
                        double mid = an/2.0;
149
                        if (mid<=alfa) {
150
                                addNode(arc, empieza+mid);
151
                        } else {
152
                                addNode(arc, mid-alfa);
153
                        }
154
                }
155
                return arc;
156
        }
157
        
158
        private void addNode(Vector<double[]> arc, double angulo) {
159
                double yy = center[1] + radio * Math.sin(angulo*Math.PI/180.0);
160
                double xx = center[0] + radio * Math.cos(angulo*Math.PI/180.0);        
161
                arc.add(new double[]{xx, yy});
162
        //        arc.add(new Point2D.Double(xx,yy));
163
        }
164
}