Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / beans / canvas / layers / functions / SquareRootPowLine.java @ 20057

History | View | Annotate | Download (6.65 KB)

1 19957 bsanchez
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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.raster.beans.canvas.layers.functions;
20
21
import java.awt.Color;
22
import java.awt.Cursor;
23
import java.awt.event.MouseEvent;
24
import java.util.ArrayList;
25
26
import org.gvsig.raster.beans.canvas.layers.InfoLayer;
27
import org.gvsig.raster.util.MathUtils;
28
import org.gvsig.raster.util.RasterToolsUtil;
29
/**
30
 * Representa una funcion con posibilidad de arrastre para las funciones
31
 * raices cuadradas y cuadradas. Con el raton se puede pasar de una a otra
32
 * directamente.
33
 *
34
 * Las formas mas logicas de uso seria pasandole:
35
 * 1.0: Para una funcion raiz cuadrada
36
 * -1.0: Para una funcion cuadrada
37
 * 0.0: Para una funcion lineal
38
 *
39
 * @version 02/04/2008
40
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
41
 */
42
public class SquareRootPowLine extends StraightLine {
43
        /**
44
         * Numero de puntos que contiene esta funci?n
45
         */
46
        private double num = 40.0;
47
48 20057 bsanchez
        private double valueFunction = 1.0;
49 19957 bsanchez
50
        /**
51
         * Constructor. Asigna el color y establece la posicion de la funcion.
52
         * Los valores normales son 1.0 para raiz cuadrada y -1.0 para cuadrada.
53
         * El rango va desde -2.0 hasta 2.0. Siendo 0.0 una funcion lineal.
54
         * @param c
55
         */
56
        public SquareRootPowLine(Color c, double point) {
57
                super(c);
58
                setShowSquares(false);
59 20057 bsanchez
                valueFunction = point;
60
                recalcList();
61 19957 bsanchez
        }
62
63
        /**
64
         * Actualiza la barra informativa para saber en que estado se encuentra el
65
         * componente.
66
         * Cuando el porcentaje es mayor a 0 siempre estamos en la raiz cuadrada
67
         * Cuando el porcentaje es menor a 0 siempre estamos en la cuadrada
68
         * @param perc
69
         */
70
        private void setInfoPoint(Double perc) {
71
                if (canvas != null) {
72
                        ArrayList list = canvas.getDrawableElements(InfoLayer.class);
73
                        if (list.size() > 0) {
74
                                InfoLayer infoLayer = (InfoLayer) list.get(0);
75
76
                                if (perc == null) {
77
                                        infoLayer.setStatusLeft(null);
78
                                        infoLayer.setStatusRight(null);
79
                                        canvas.repaint();
80
                                        return;
81
                                }
82
83
                                if (perc.doubleValue() > 0.0)
84
                                        infoLayer.setStatusLeft(RasterToolsUtil.getText(this, "square_root"));
85
                                else
86
                                        infoLayer.setStatusLeft(RasterToolsUtil.getText(this, "pow"));
87
88
                                infoLayer.setStatusRight(MathUtils.clipDecimals(Math.abs(perc.doubleValue() * 100.0), 2) + "%");
89
                        }
90
                }
91
        }
92
93
        /**
94
         * Recalcula todos los puntos de la funcion
95
         * Posibles rangos para perc:
96
         * ( 0.0 a  1.0) - Funcion raiz cuadrada con aproximacion al centro
97
         * ( 1.0 a  2.0) - Funcion raiz cuadrada con aproximacion al borde
98
         * ( 0.0 a -1.0) - Funcion cuadrada con aproximacion al centro
99
         * (-1.0 a -2.0) - Funcion cuadrada con aproximacion al borde
100
         *
101
         * @param perc
102
         */
103 20057 bsanchez
        private void recalcList() {
104 19957 bsanchez
                double x, y = 0.0;
105
106 20057 bsanchez
                setInfoPoint(new Double(valueFunction));
107 19957 bsanchez
108
                this.listSquare.clear();
109
110
                for (int i = 0; i <= num; i++) {
111
                        x = ((double) i) / num;
112
113
                        // Aproximacion al centro de una funcion raiz cuadrada
114 20057 bsanchez
                        if (valueFunction >= 0.0 && valueFunction <= 1.0) {
115 19957 bsanchez
                                y = squareFunction(x);
116 20057 bsanchez
                                y = x + ((y - x) * valueFunction);
117 19957 bsanchez
                        }
118
119
                        // Aproximacion al borde de una funcion raiz cuadrada
120 20057 bsanchez
                        if (valueFunction > 1.0 && valueFunction <= 2.0) {
121 19957 bsanchez
                                y = squareFunction(x);
122 20057 bsanchez
                                y = y + ((1.0 - y) * (valueFunction - 1.0));
123 19957 bsanchez
                        }
124
125
                        // Aproximacion al centro de una funcion cuadrada
126 20057 bsanchez
                        if (valueFunction >= -1.0 && valueFunction < 0.0) {
127 19957 bsanchez
                                y = powFunction(x);
128 20057 bsanchez
                                y = x - ((x - y) * Math.abs(valueFunction));
129 19957 bsanchez
                        }
130
131
                        // Aproximacion al borde de una funcion cuadrada
132 20057 bsanchez
                        if (valueFunction >= -2.0 && valueFunction < -1.0) {
133 19957 bsanchez
                                y = powFunction(x);
134 20057 bsanchez
                                y = y * (1.0 - (Math.abs(valueFunction) - 1.0));
135 19957 bsanchez
                        }
136
137
                        if (y < 0.0)
138
                                y = 0.0;
139
                        if (y > 1.0)
140
                                y = 1.0;
141
142
                        this.listSquare.add(new Square(x, y));
143
                }
144
        }
145
146
        /**
147
         * Formula para calcular el valor en y de una funcion raiz cuadrada en x
148
         * @param x
149
         * @return
150
         */
151
        private double squareFunction(double x) {
152
                return Math.sqrt(x);
153
        }
154
155
        /**
156
         * Formula para calcular el valor en y de una funcion cuadrada en x
157
         * @param x
158
         * @return
159
         */
160
        private double powFunction(double x) {
161
                return Math.pow(x, 2);
162
        }
163
164
        /* (non-Javadoc)
165
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseDragged(java.awt.event.MouseEvent)
166
         */
167
        public boolean mouseDragged(MouseEvent e) {
168
                if (canvas.getCursor().getType() != Cursor.DEFAULT_CURSOR)
169
                        return true;
170
171
                double x = pixelToValueX(e.getX());
172
                double y = pixelToValueY(e.getY());
173
174
                double y2 = 0.0;
175
176
                // Localizamos el punto del raton para pasar un porcentaje correcto y que
177
                // asi coincida la funcion con el raton
178
                if (y >= x) {
179
                        y2 = squareFunction(x);
180
                        if (y < y2)
181 20057 bsanchez
                                valueFunction = (y - x) / (y2 - x);
182 19957 bsanchez
                        else
183 20057 bsanchez
                                valueFunction = ((y - y2) / (1.0 - y2)) + 1.0;
184 19957 bsanchez
                } else {
185
                        y2 = powFunction(x);
186
187
                        if (y > y2)
188 20057 bsanchez
                                valueFunction = -Math.abs((y - x) / (y2 - x));
189 19957 bsanchez
                        else
190 20057 bsanchez
                                valueFunction = -Math.abs((y2 - y) / y2)  - 1.0;
191 19957 bsanchez
                }
192
193 20057 bsanchez
                if (valueFunction < -2.0)
194
                        valueFunction = -2.0;
195 19957 bsanchez
196 20057 bsanchez
                if (valueFunction > 2.0)
197
                        valueFunction = 2.0;
198 19957 bsanchez
199 20057 bsanchez
                recalcList();
200 19957 bsanchez
                canvas.repaint();
201
                canvas.callDataDragged("line", this);
202
                return false;
203
        }
204
205
        /* (non-Javadoc)
206
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseMoved(java.awt.event.MouseEvent)
207
         */
208
        public boolean mouseMoved(MouseEvent e) {
209
                return true;
210
        }
211
212
        /* (non-Javadoc)
213
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mousePressed(java.awt.event.MouseEvent)
214
         */
215
        public boolean mousePressed(MouseEvent e) {
216
                return mouseDragged(e);
217
        }
218
219
        /* (non-Javadoc)
220
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseReleased(java.awt.event.MouseEvent)
221
         */
222
        public boolean mouseReleased(MouseEvent e) {
223
                setInfoPoint(null);
224
                return true;
225
        }
226 20057 bsanchez
227
        /*
228
         * (non-Javadoc)
229
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#getFunctionType()
230
         */
231
        public int getFunctionType() {
232
                return 2;
233
        }
234
235
        /*
236
         * (non-Javadoc)
237
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#getValueFunction()
238
         */
239
        public double getValueFunction() {
240
                return valueFunction;
241
        }
242 19957 bsanchez
}