Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap / src / org / gvsig / fmap / core / shapes / FArc2D.java @ 20989

History | View | Annotate | Download (11.4 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.core.shapes;
46

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

    
53
import org.gvsig.fmap.core.shapes.handlers.AbstractHandler;
54
import org.gvsig.fmap.core.shapes.handlers.Handler;
55
import org.gvsig.fmap.core.shapes.handlers.ICenterHandler;
56
import org.gvsig.fmap.core.shapes.handlers.IFinalHandler;
57
import org.gvsig.fmap.drivers.writing.UtilFunctions;
58

    
59

    
60

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

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

    
133
                handlers.add(new InitHandler(0, init.getX(), init.getY()));
134
                //handlers.add(new CenterHandler(1, center.getX(), center.getY()));
135
                handlers.add(new EndHandler(1, end.getX(), end.getY()));
136

    
137
                return (Handler[]) handlers.toArray(new Handler[0]);
138
        }
139

    
140
        public Handler[] getSelectHandlers() {
141
                ArrayList handlers = new ArrayList();
142

    
143
                handlers.add(new InitSelHandler(0, init.getX(), init.getY()));
144
                handlers.add(new CenterSelHandler(1, center.getX(), center.getY()));
145
                handlers.add(new EndSelHandler(2, end.getX(), end.getY()));
146

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

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

    
184
                /**
185
                 * @see org.gvsig.fmap.core.shapes.handlers.Handler#set(double, double)
186
                 */
187
                public void set(double x, double y) {
188
                        center=new Point2D.Double(x,y);
189
                        Arc2D arco = UtilFunctions.createArc(init,center, end);
190
                        gp=new GeneralPathX(arco);
191
                }
192
        }
193

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

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

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

    
232
                        Arc2D arco = UtilFunctions.createArc(init,center, end);
233
                        gp=new GeneralPathX(arco);
234
                }
235
                public void setPoint(Point2D p){
236
                        init=p;
237
                }
238
                public Point2D getPoint(){
239
                        return init;
240
                }
241

    
242
                /**
243
                 * @see org.gvsig.fmap.core.shapes.handlers.Handler#set(double, double)
244
                 */
245
                public void set(double x, double y) {
246
                        }
247
        }
248

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

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

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

    
287
                        Arc2D arco = UtilFunctions.createArc(init,center, end);
288
                        gp=new GeneralPathX(arco);
289
                }
290
                public void setPoint(Point2D p){
291
                        end=p;
292
                }
293
                public Point2D getPoint(){
294
                        return end;
295
                }
296

    
297
                /**
298
                 * @see org.gvsig.fmap.core.shapes.handlers.Handler#set(double, double)
299
                 */
300
                public void set(double x, double y) {
301

    
302
                }
303
        }
304

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

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

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

    
343
                        Arc2D arco = UtilFunctions.createArc(init,center, end);
344
                        gp=new GeneralPathX(arco);
345
                }
346
                public void setPoint(Point2D p){
347
                        init=p;
348
                }
349
                public Point2D getPoint(){
350
                        return init;
351
                }
352

    
353
                /**
354
                 * @see org.gvsig.fmap.core.shapes.handlers.Handler#set(double, double)
355
                 */
356
                public void set(double x, double y) {
357
                        Point2D mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
358
                        double dist=mediop.distance(center);
359
                        init=new Point2D.Double(x,y);
360

    
361
                        mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
362
                        Point2D[] perp=UtilFunctions.getPerpendicular(init,end,mediop);
363
                        if (UtilFunctions.getAngle(end,init)<=Math.PI){
364
                                dist=-dist;
365
                        }
366
                        ///center=TrigonometricalFunctions.getPoint(mediop,perp[1],dist);
367
                        Arc2D arco = UtilFunctions.createArc(init,center, end);
368
                        gp=new GeneralPathX(arco);
369
                }
370
        }
371

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

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

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

    
410
                        Arc2D arco = UtilFunctions.createArc(init,center, end);
411
                        gp=new GeneralPathX(arco);
412
                }
413
                public void setPoint(Point2D p){
414
                        end=p;
415
                }
416
                public Point2D getPoint(){
417
                        return end;
418
                }
419

    
420
                /**
421
                 * @see org.gvsig.fmap.core.shapes.handlers.Handler#set(double, double)
422
                 */
423
                public void set(double x, double y) {
424
                        Point2D mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
425
                        double dist=mediop.distance(center);
426
                        end=new Point2D.Double(x,y);
427

    
428
                        mediop=new Point2D.Double((init.getX()+end.getX())/2,(init.getY()+end.getY())/2);
429
                        Point2D[] perp=UtilFunctions.getPerpendicular(init,end,mediop);
430
                        if (UtilFunctions.getAngle(end,init)<=Math.PI){
431
                                dist=-dist;
432
                        }
433
                        ///center=TrigonometricalFunctions.getPoint(mediop,perp[1],dist);
434
                        Arc2D arco = UtilFunctions.createArc(init,center, end);
435
                        gp=new GeneralPathX(arco);
436
                }
437
        }
438

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

    
446

    
447
}