Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / primitive / curve / arc / Arc2D.java @ 42267

History | View | Annotate | Download (5.7 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.geom.jts.primitive.curve.arc;
24

    
25
import java.awt.geom.PathIterator;
26

    
27
import com.vividsolutions.jts.geom.Coordinate;
28

    
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
31
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS;
32
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
33
import org.gvsig.fmap.geom.jts.util.JTSUtils;
34
import org.gvsig.fmap.geom.primitive.Point;
35

    
36

    
37

    
38
/**
39
 * @author fdiaz
40
 *
41
 */
42
public class Arc2D extends AbstractArc {
43

    
44
    /**
45
     *
46
     */
47
    private static final long serialVersionUID = 3414562338763885954L;
48

    
49
    /**
50
     * @param subtype
51
     */
52
    protected Arc2D() {
53
        super(Geometry.SUBTYPES.GEOM2D);
54
    }
55

    
56
    /* (non-Javadoc)
57
     * @see org.gvsig.fmap.geom.primitive.Arc#setPoints(org.gvsig.fmap.geom.primitive.Point, double, double, double)
58
     */
59
    public void setPoints(Point center, double radius, double startAngle, double angleExt) {
60

    
61
        double x;
62
        double y;
63
        double w;
64
        double h;
65
        double start;
66
        double extent;
67
        double angleOffset;
68
        final double pi2 = Math.PI*2;
69

    
70
        angleOffset = 0;
71
        if( angleExt<0 ) {
72
            angleOffset = pi2 + angleExt;
73
            angleExt = Math.abs(angleExt);
74
        }
75
        x = center.getX() - radius;
76
        y = center.getY() - radius;
77
        w = radius * 2;
78
        h = w; // Son siempre arcos de circunferencia
79

    
80
        if( angleExt > 0 && (angleExt % pi2) == 0  ) {
81
            start = 0;
82
            extent = 360;
83
        } else {
84
            angleExt = angleExt % pi2; //Asumimos que aqui angleExt es siempre positivo
85
            startAngle = Math.abs(startAngle) % pi2;
86
            start = Math.toDegrees( pi2 - startAngle + angleOffset) ;
87
            extent = - Math.toDegrees(pi2 - angleExt);
88
        }
89

    
90

    
91
        double angleStart = Math.toRadians(-start);
92
        double initX = x + (Math.cos(angleStart) * 0.5 + 0.5) * radius*2;
93
        double initY = y + (Math.sin(angleStart) * 0.5 + 0.5) * radius*2;
94
        init = new Point2D(initX, initY);
95

    
96
        double angleEnd = Math.toRadians(-start - extent);
97
        double endX = x + (Math.cos(angleEnd) * 0.5 + 0.5) * radius*2;
98
        double endY = y + (Math.sin(angleEnd) * 0.5 + 0.5) * radius*2;
99
        end =  new Point2D(endX, endY);
100

    
101
        double angleMiddle = Math.toRadians(-start - extent)/2;
102
        double middleX = x + (Math.cos(angleMiddle) * 0.5 + 0.5) * radius*2;
103
        double middleY = y + (Math.sin(angleMiddle) * 0.5 + 0.5) * radius*2;
104
        end =  new Point2D(middleX, middleY);
105
}
106

    
107
    /* (non-Javadoc)
108
     * @see org.gvsig.fmap.geom.primitive.Arc#getCenterPoint()
109
     */
110
    public Point getCenterPoint() {
111
        ((PointJTS)init).getJTS();
112
        Point2D center = new Point2D(JTSUtils.getCircumcentre(init, middle, end));
113
        return center;
114
    }
115

    
116
    /* (non-Javadoc)
117
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
118
     */
119
    public Geometry cloneGeometry() {
120
        Arc2D arc2D = new Arc2D();
121
        arc2D.setPoints(init, middle, end);
122
        return arc2D;
123
    }
124

    
125
    /*
126
     * (non-Javadoc)
127
     *
128
     * @see
129
     * org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#fixPoint(org
130
     * .gvsig.fmap.geom.primitive.Point)
131
     */
132
    @Override
133
    protected Point fixPoint(Point point) {
134
        if (point instanceof Point2D) {
135
            return point;
136
        } else {
137
            return new Point2D(point.getX(), point.getY());
138
        }
139
    }
140

    
141
    /* (non-Javadoc)
142
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
143
     */
144
    public com.vividsolutions.jts.geom.Geometry getJTS() {
145
        PathIterator pi = getPathIterator(null);
146
        ArrayListCoordinateSequence coordinates = new ArrayListCoordinateSequence();
147

    
148
        double coords[] = new double[6];
149
        while (!pi.isDone()) {
150
            switch (pi.currentSegment(coords)) {
151
            case PathIterator.SEG_MOVETO:
152
                coordinates.add(new Coordinate(coords[0], coords[1]));
153
                break;
154
            case PathIterator.SEG_LINETO:
155
                coordinates.add(new Coordinate(coords[0], coords[1]));
156
                break;
157
            case PathIterator.SEG_QUADTO:
158
                coordinates.add(new Coordinate(coords[0], coords[1]));
159
                coordinates.add(new Coordinate(coords[2], coords[3]));
160
                break;
161
            case PathIterator.SEG_CUBICTO:
162
                coordinates.add(new Coordinate(coords[0], coords[1]));
163
                coordinates.add(new Coordinate(coords[2], coords[3]));
164
                coordinates.add(new Coordinate(coords[4], coords[5]));
165
                break;
166
            case PathIterator.SEG_CLOSE:
167
                coordinates.add(coordinates.get(0));
168
                break;
169
            }
170
            pi.next();
171
        }
172
        return JTSUtils.createJTSLinearRing(coordinates);
173
    }
174

    
175

    
176
}