Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / vectorizacion / fmap / BezierPathX.java @ 23307

History | View | Annotate | Download (3.64 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.rastertools.vectorizacion.fmap;
20

    
21
import java.awt.geom.Point2D;
22

    
23
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
24
/**
25
 * Reimplementacion del GeneralPathX para que acepte curvas de Bezier
26
 * 
27
 * @version 28/08/2008
28
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
29
 */
30
public class BezierPathX extends GeneralPathX {
31
        private static final long serialVersionUID = -77239133302246295L;
32
        private Point2D lastPoint      = new Point2D.Double(0, 0);
33
        private int     numberOfPoints = 1;
34

    
35
        /**
36
         * Crea un GeneralPathX definiendo cuantos cortes tendr? en las curvas de
37
         * Bezier. Cuanto mayor sea el numero de puntos, mejor definido estar?
38
         * @param numberOfPoints
39
         */
40
        public BezierPathX(int numberOfPoints) {
41
                this.numberOfPoints = numberOfPoints;
42
        }
43

    
44
        /**
45
         * Devuelve el punto de la curva de Bezier para el intervalo t
46
         * @param cp
47
         * @param t
48
         * @return
49
         */
50
        private Point2D pointOnCubicBezier(Point2D[] cp, double t) {
51
                double ax, bx, cx;
52
                double ay, by, cy;
53
                double tSquared, tCubed;
54
                Point2D result = new Point2D.Double();
55

    
56
                /* c?lculo de los coeficientes polinomiales */
57

    
58
                cx = 3.0 * (cp[1].getX() - cp[0].getX());
59
                bx = 3.0 * (cp[2].getX() - cp[1].getX()) - cx;
60
                ax = cp[3].getX() - cp[0].getX() - cx - bx;
61

    
62
                cy = 3.0 * (cp[1].getY() - cp[0].getY());
63
                by = 3.0 * (cp[2].getY() - cp[1].getY()) - cy;
64
                ay = cp[3].getY() - cp[0].getY() - cy - by;
65

    
66
                /* calculate the curve point at parameter value t */
67

    
68
                tSquared = t * t;
69
                tCubed = tSquared * t;
70

    
71
                result.setLocation((ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].getX(), (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].getY());
72

    
73
                return result;
74
        }
75

    
76
        /*
77
         * (non-Javadoc)
78
         * @see com.iver.cit.gvsig.fmap.core.GeneralPathX#curveTo(double, double, double, double, double, double)
79
         */
80
        public synchronized void curveTo(double x1, double y1, double x2, double y2, double x3, double y3) {
81
                Point2D points[] = new Point2D[4];
82
                points[0] = new Point2D.Double(lastPoint.getX(), lastPoint.getY());
83
                points[1] = new Point2D.Double(x1, y1);
84
                points[2] = new Point2D.Double(x2, y2);
85
                points[3] = new Point2D.Double(x3, y3);
86

    
87
                double dt = 1.0 / (numberOfPoints - 1);
88

    
89
                Point2D aux;
90
                for (int i = 0; i < numberOfPoints; i++) {
91
                        aux = pointOnCubicBezier(points, i * dt);
92
                        lineTo(aux.getX(), aux.getY());
93
                }
94
        }
95

    
96
        /*
97
         * (non-Javadoc)
98
         * @see com.iver.cit.gvsig.fmap.core.GeneralPathX#lineTo(double, double)
99
         */
100
        public synchronized void lineTo(double x, double y) {
101
                super.lineTo(x, y);
102
                lastPoint.setLocation(x, y);
103
        }
104

    
105
        /*
106
         * (non-Javadoc)
107
         * @see com.iver.cit.gvsig.fmap.core.GeneralPathX#moveTo(double, double)
108
         */
109
        public synchronized void moveTo(double x, double y) {
110
                super.moveTo(x, y);
111
                lastPoint.setLocation(x, y);
112
        }
113
}