Statistics
| Revision:

root / trunk / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / Arc2D.java @ 20859

History | View | Annotate | Download (11.6 KB)

1
/*
2
 * Created on 09-feb-2005
3
 *
4
 * gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. 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,
21
   USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib��ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package org.gvsig.fmap.geom.primitive;
46

    
47
import java.awt.geom.AffineTransform;
48
import java.awt.geom.Rectangle2D;
49
import java.awt.geom.Point2D;
50
import java.util.ArrayList;
51

    
52
import org.cresques.cts.IProjection;
53
import org.gvsig.fmap.geom.handler.AbstractHandler;
54
import org.gvsig.fmap.geom.handler.CenterHandler;
55
import org.gvsig.fmap.geom.handler.FinalHandler;
56
import org.gvsig.fmap.geom.handler.Handler;
57
import org.gvsig.fmap.geom.util.UtilFunctions;
58

    
59

    
60

    
61
/**
62
 * DOCUMENT ME!
63
 *
64
 * @author Vicente Caballero Navarro
65
 */
66
public class Arc2D extends Curve2D {
67

    
68
        private static final long serialVersionUID = 1L;
69

    
70
        private Point2D init;
71
        private Point2D center;
72
        private Point2D end;
73
        /**
74
         * DOCUMENT ME!
75
         *
76
         * @param gpx
77
         */
78
        public Arc2D(String id, IProjection projection, GeneralPathX gpx, Point2D i,Point2D c, Point2D e) {
79
                super(id, projection, gpx);
80
                init=i;
81
                center=c;
82
                end=e;
83
        }
84
        public Point2D getInit(){
85
                return init;
86
        }
87
        public Point2D getEnd(){
88
                return end;
89
        }
90
        public Point2D getCenter(){
91
                return UtilFunctions.getCenter(init, center,end);
92
        }
93
        public Point2D getMid(){
94
                return center;
95
        }
96
        /* (non-Javadoc)
97
         * @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
98
         */
99
        public FShape cloneFShape() {
100
                Arc2D arc=new Arc2D(id, projection, (GeneralPathX) gp.clone(),init,center,end);
101
                return arc;
102
        }
103
        public void transform(AffineTransform at) {
104
                gp.transform(at);
105
                InitHandler inithandler=(InitHandler)getStretchingHandlers()[0];
106
                //CenterHandler centerhandler=(CenterHandler)getHandlers()[1];
107
                EndHandler endhandler=(EndHandler)getStretchingHandlers()[1];
108
                Point2D aux1=new Point2D.Double();
109
                Point2D aux2=new Point2D.Double();
110
                Point2D aux3=new Point2D.Double();
111
                at.transform(inithandler.getPoint(),aux1);
112
                inithandler.setPoint(aux1);
113
                //at.transform(centerhandler.getPoint(),aux2);
114
                //centerhandler.setPoint(aux2);
115
                at.transform(endhandler.getPoint(),aux3);
116
                endhandler.setPoint(aux3);
117
                CenterSelHandler centerhandler=(CenterSelHandler)getSelectHandlers()[1];
118
                at.transform(centerhandler.getPoint(),aux2);
119
                centerhandler.setPoint(aux2);
120

    
121
        }
122
        /**
123
         * @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
124
         */
125
        public int getShapeType() {
126
                return FShape.ARC;
127
        }
128
        /**
129
         * DOCUMENT ME!
130
         *
131
         * @return DOCUMENT ME!
132
         */
133
        public Handler[] getStretchingHandlers() {
134
                ArrayList handlers = new ArrayList();
135

    
136
                handlers.add(new InitHandler(0, init.getX(), init.getY()));
137
                //handlers.add(new CenterHandler(1, center.getX(), center.getY()));
138
                handlers.add(new EndHandler(1, end.getX(), end.getY()));
139

    
140
                return (Handler[]) handlers.toArray(new Handler[0]);
141
        }
142

    
143
        public Handler[] getSelectHandlers() {
144
                ArrayList handlers = new ArrayList();
145

    
146
                handlers.add(new InitSelHandler(0, init.getX(), init.getY()));
147
                handlers.add(new CenterSelHandler(1, center.getX(), center.getY()));
148
                handlers.add(new EndSelHandler(2, end.getX(), end.getY()));
149

    
150
                return (Handler[]) handlers.toArray(new Handler[0]);
151
        }
152
        /**
153
         * DOCUMENT ME!
154
         *
155
         * @author Vicente Caballero Navarro
156
         */
157
        class CenterSelHandler extends AbstractHandler implements CenterHandler{
158
                /**
159
                 * Crea un nuevo PointHandler.
160
                 *
161
                 * @param i DOCUMENT ME!
162
                 * @param x DOCUMENT ME!
163
                 * @param y DOCUMENT ME!
164
                 */
165
                public CenterSelHandler(int i, double x, double y) {
166
                        center = new Point2D.Double(x, y);
167
                        index = i;
168
                }
169

    
170
                /**
171
                 * DOCUMENT ME!
172
                 *
173
                 * @param x DOCUMENT ME!
174
                 * @param y DOCUMENT ME!
175
                 *
176
                 * @return DOCUMENT ME!
177
                 */
178
                public void move(double x, double y) {
179
                }
180
                public void setPoint(Point2D p){
181
                        center=p;
182
                }
183
                public Point2D getPoint(){
184
                        return center;
185
                }
186

    
187
                /**
188
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
189
                 */
190
                public void set(double x, double y) {
191
                        center=new Point2D.Double(x,y);
192
                        java.awt.geom.Arc2D arco = UtilFunctions.createArc(init, center, end);
193
                        gp = new GeneralPathX(arco);
194
                }
195
        }
196

    
197
        /**
198
         * DOCUMENT ME!
199
         *
200
         * @author Vicente Caballero Navarro
201
         */
202
        class InitHandler extends AbstractHandler implements FinalHandler{
203
                /**
204
                 * Crea un nuevo PointHandler.
205
                 *
206
                 * @param i DOCUMENT ME!
207
                 * @param x DOCUMENT ME!
208
                 * @param y DOCUMENT ME!
209
                 */
210
                public InitHandler(int i, double x, double y) {
211
                        init = new Point2D.Double(x, y);
212
                        index = i;
213
                }
214

    
215
                /**
216
                 * DOCUMENT ME!
217
                 *
218
                 * @param x DOCUMENT ME!
219
                 * @param y DOCUMENT ME!
220
                 *
221
                 * @return DOCUMENT ME!
222
                 */
223
                public void move(double x, double y) {
224
                        Point2D mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
225
                        double dist=mediop.distance(center);
226
                        init=new Point2D.Double(init.getX()+x,init.getY()+y);
227

    
228
                        mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
229
                        Point2D[] perp=UtilFunctions.getPerpendicular(init,end,mediop);
230
                        if (UtilFunctions.getAngle(end,init)<=Math.PI){
231
                                dist=-dist;
232
                        }
233
                        center=UtilFunctions.getPoint(mediop,perp[1],dist);
234

    
235
                        java.awt.geom.Arc2D arco = UtilFunctions.createArc(init,center, end);
236
                        gp=new GeneralPathX(arco);
237
                }
238
                public void setPoint(Point2D p){
239
                        init=p;
240
                }
241
                public Point2D getPoint(){
242
                        return init;
243
                }
244

    
245
                /**
246
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
247
                 */
248
                public void set(double x, double y) {
249
                        }
250
        }
251

    
252
        /**
253
         * DOCUMENT ME!
254
         *
255
         * @author Vicente Caballero Navarro
256
         */
257
        class EndHandler extends AbstractHandler implements FinalHandler{
258
                /**
259
                 * Crea un nuevo PointHandler.
260
                 *
261
                 * @param i DOCUMENT ME!
262
                 * @param x DOCUMENT ME!
263
                 * @param y DOCUMENT ME!
264
                 */
265
                public EndHandler(int i, double x, double y) {
266
                        end = new Point2D.Double(x, y);
267
                        index = i;
268
                }
269

    
270
                /**
271
                 * DOCUMENT ME!
272
                 *
273
                 * @param x DOCUMENT ME!
274
                 * @param y DOCUMENT ME!
275
                 *
276
                 * @return DOCUMENT ME!
277
                 */
278
                public void move(double x, double y) {
279
                        Point2D mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
280
                        double dist=mediop.distance(center);
281
                        end=new Point2D.Double(end.getX()+x,end.getY()+y);
282

    
283
                        mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
284
                        Point2D[] perp=UtilFunctions.getPerpendicular(init,end,mediop);
285
                        if (UtilFunctions.getAngle(end,init)<=Math.PI){
286
                                dist=-dist;
287
                        }
288
                        center=UtilFunctions.getPoint(mediop,perp[1],dist);
289

    
290
                        java.awt.geom.Arc2D arco = UtilFunctions.createArc(init,center, end);
291
                        gp=new GeneralPathX(arco);
292
                }
293
                public void setPoint(Point2D p){
294
                        end=p;
295
                }
296
                public Point2D getPoint(){
297
                        return end;
298
                }
299

    
300
                /**
301
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
302
                 */
303
                public void set(double x, double y) {
304

    
305
                }
306
        }
307

    
308
        /**
309
         * DOCUMENT ME!
310
         *
311
         * @author Vicente Caballero Navarro
312
         */
313
        class InitSelHandler extends AbstractHandler implements FinalHandler{
314
                /**
315
                 * Crea un nuevo PointHandler.
316
                 *
317
                 * @param i DOCUMENT ME!
318
                 * @param x DOCUMENT ME!
319
                 * @param y DOCUMENT ME!
320
                 */
321
                public InitSelHandler(int i, double x, double y) {
322
                        init = new Point2D.Double(x, y);
323
                        index = i;
324
                }
325

    
326
                /**
327
                 * DOCUMENT ME!
328
                 *
329
                 * @param x DOCUMENT ME!
330
                 * @param y DOCUMENT ME!
331
                 *
332
                 * @return DOCUMENT ME!
333
                 */
334
                public void move(double x, double y) {
335
                        Point2D mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
336
                        double dist=mediop.distance(center);
337
                        init=new Point2D.Double(init.getX()+x,init.getY()+y);
338

    
339
                        mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
340
                        Point2D[] perp=UtilFunctions.getPerpendicular(init,end,mediop);
341
                        if (UtilFunctions.getAngle(end,init)<=Math.PI){
342
                                dist=-dist;
343
                        }
344
                        center=UtilFunctions.getPoint(mediop,perp[1],dist);
345

    
346
                        java.awt.geom.Arc2D arco = UtilFunctions.createArc(init,center, end);
347
                        gp=new GeneralPathX(arco);
348
                }
349
                public void setPoint(Point2D p){
350
                        init=p;
351
                }
352
                public Point2D getPoint(){
353
                        return init;
354
                }
355

    
356
                /**
357
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
358
                 */
359
                public void set(double x, double y) {
360
                        Point2D mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
361
                        double dist=mediop.distance(center);
362
                        init=new Point2D.Double(x,y);
363

    
364
                        mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
365
                        // TODO comentado para quitar warning: Point2D[] perp=UtilFunctions.getPerpendicular(init,end,mediop);
366
                        if (UtilFunctions.getAngle(end,init)<=Math.PI){
367
                                dist=-dist;
368
                        }
369
                        ///center=TrigonometricalFunctions.getPoint(mediop,perp[1],dist);
370
                        java.awt.geom.Arc2D arco = UtilFunctions.createArc(init,center, end);
371
                        gp=new GeneralPathX(arco);
372
                }
373
        }
374

    
375
        /**
376
         * DOCUMENT ME!
377
         *
378
         * @author Vicente Caballero Navarro
379
         */
380
        class EndSelHandler extends AbstractHandler implements FinalHandler{
381
                /**
382
                 * Crea un nuevo PointHandler.
383
                 *
384
                 * @param i DOCUMENT ME!
385
                 * @param x DOCUMENT ME!
386
                 * @param y DOCUMENT ME!
387
                 */
388
                public EndSelHandler(int i, double x, double y) {
389
                        end = new Point2D.Double(x, y);
390
                        index = i;
391
                }
392

    
393
                /**
394
                 * DOCUMENT ME!
395
                 *
396
                 * @param x DOCUMENT ME!
397
                 * @param y DOCUMENT ME!
398
                 *
399
                 * @return DOCUMENT ME!
400
                 */
401
                public void move(double x, double y) {
402
                        Point2D mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
403
                        double dist=mediop.distance(center);
404
                        end=new Point2D.Double(end.getX()+x,end.getY()+y);
405

    
406
                        mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
407
                        Point2D[] perp=UtilFunctions.getPerpendicular(init,end,mediop);
408
                        if (UtilFunctions.getAngle(end,init)<=Math.PI){
409
                                dist=-dist;
410
                        }
411
                        center=UtilFunctions.getPoint(mediop,perp[1],dist);
412

    
413
                        java.awt.geom.Arc2D arco = UtilFunctions.createArc(init,center, end);
414
                        gp=new GeneralPathX(arco);
415
                }
416
                public void setPoint(Point2D p){
417
                        end=p;
418
                }
419
                public Point2D getPoint(){
420
                        return end;
421
                }
422

    
423
                /**
424
                 * @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
425
                 */
426
                public void set(double x, double y) {
427
                        Point2D mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
428
                        double dist=mediop.distance(center);
429
                        end=new Point2D.Double(x,y);
430

    
431
                        mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
432
                        // TODO comentado para quitar warning: Point2D[] perp=UtilFunctions.getPerpendicular(init,end,mediop);
433
                        if (UtilFunctions.getAngle(end,init)<=Math.PI){
434
                                dist=-dist;
435
                        }
436
                        ///center=TrigonometricalFunctions.getPoint(mediop,perp[1],dist);
437
                        java.awt.geom.Arc2D arco = UtilFunctions.createArc(init,center, end);
438
                        gp=new GeneralPathX(arco);
439
                }
440
        }
441

    
442
        /* (non-Javadoc)
443
         * @see com.iver.cit.gvsig.fmap.core.FPolyline2D#intersects(java.awt.geom.Rectangle2D)
444
         */
445
        public boolean intersects(Rectangle2D r) {
446
                return gp.intersects(r);
447
        }
448

    
449

    
450
}