Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libDwg / src / com / iver / cit / jdwglib / util / ArcFromBulgeCalculator.java @ 10539

History | View | Annotate | Download (4.76 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 Point2D coord1, coord2;
47
        private Point2D center;
48
        private double radio, empieza, acaba;
49
        private double bulge;
50
        private double d, dd, aci;
51
        private Point2D 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(Point2D p1, Point2D 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.getX()-coord1.getX())*(coord2.getX()-coord1.getX()) + (coord2.getY()-coord1.getY())*(coord2.getY()-coord1.getY()));
74
                coordAux = new Point2D.Double((coord1.getX()+coord2.getX())/2.0, (coord1.getY()+coord2.getY())/2.0);
75
                double b = Math.abs(bulge);
76
                double beta = Math.atan(b);
77
                double alfa = beta*4.0;
78
                double landa = alfa/2.0;
79
                dd = (d/2.0)/(Math.tan(landa));
80
                radio = (d/2.0)/(Math.sin(landa));                
81
                aci = Math.atan((coord2.getX()-coord1.getX())/(coord2.getY()-coord1.getY()));
82
                double aciDegree = aci*180.0/Math.PI;
83
                if (coord2.getY() > coord1.getY()) {
84
                        aci += Math.PI;
85
                        aciDegree = aci*180.0/Math.PI;
86
                }
87
                center = new Point2D.Double(coordAux.getX() + dd*Math.sin(aci+(Math.PI/2.0)), coordAux.getY() + dd*Math.cos(aci+(Math.PI/2.0)));
88
                calEA(alfa);
89
        }
90
        
91
        private void calEA(double alfa){
92
                empieza = Math.atan2(coord1.getY()-center.getY(), coord1.getX()-center.getX());
93
                acaba = (empieza + alfa);
94
                empieza = empieza*180.0/Math.PI;
95
                acaba = acaba*180.0/Math.PI;
96
        }
97
        
98
        /**
99
         * This method calculates an arc in a Gis geometry model. This arc is represented in
100
         * this model by a Vector of Point2D. The distance between points in the arc is given
101
         * as an argument
102
         * 
103
         * @param inc Distance between points in the arc
104
         * @return Vector Vector with the set of Point2D that represents the arc
105
         */
106
        public Vector getPoints(double inc) {
107
                Vector arc = new Vector();
108
                double angulo;
109
                int iempieza = (int) empieza + 1;
110
                int iacaba = (int) acaba;
111
                if (empieza <= acaba) {
112
                        addNode(arc, empieza);
113
                        for (angulo = iempieza; angulo <= iacaba; angulo += inc) {
114
                                addNode(arc, angulo);
115
                        }
116
                        addNode(arc, acaba);
117
                } else {
118
                        addNode(arc, empieza);
119
                        for (angulo = iempieza ; angulo <= 360; angulo += inc) {
120
                                addNode(arc, angulo);
121
                        }
122
                        for (angulo = 1; angulo <= iacaba; angulo += inc) {
123
                                addNode(arc, angulo);
124
                        }
125
                        addNode(arc, angulo);
126
                }
127
                Point2D aux = (Point2D)arc.get(arc.size()-1);
128
                double aux1 = Math.abs(aux.getX()-coord2.getX());
129
                double aux2 = Math.abs(aux.getY()-coord2.getY());
130
                return arc;
131
        }
132
        
133
        /**
134
         * Method that allows to obtain a set of points located in the central zone of 
135
         * this arc object
136
         */
137
        public Vector getCentralPoint() {
138
                Vector arc = new Vector();
139
                if (empieza <= acaba) {
140
                        addNode(arc, (empieza+acaba)/2.0);
141
                } else {
142
                        addNode(arc, empieza);
143
                        double alfa = 360-empieza;
144
                        double beta = acaba;
145
                        double an = alfa + beta;
146
                        double mid = an/2.0;
147
                        if (mid<=alfa) {
148
                                addNode(arc, empieza+mid);
149
                        } else {
150
                                addNode(arc, mid-alfa);
151
                        }
152
                }
153
                return arc;
154
        }
155
        
156
        private void addNode(Vector arc, double angulo) {
157
                double yy = center.getY() + radio * Math.sin(angulo*Math.PI/180.0);
158
                double xx = center.getX() + radio * Math.cos(angulo*Math.PI/180.0);                
159
                arc.add(new Point2D.Double(xx,yy));
160
        }
161
}