Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.impl / src / main / java / org / gvsig / raster / swing / impl / canvas / layer / function / DefaultSquareRootPowLine.java @ 2584

History | View | Annotate | Download (6.94 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.swing.impl.canvas.layer.function;
23

    
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.Cursor;
27
import java.awt.event.MouseEvent;
28
import java.util.List;
29

    
30
import org.gvsig.raster.swing.gcanvas.DrawableElement;
31
import org.gvsig.raster.swing.gcanvas.SquareRootPowLine;
32
import org.gvsig.raster.swing.impl.DefaultRasterSwingManager;
33
import org.gvsig.raster.swing.impl.canvas.layer.DefaultInfoLayer;
34
/**
35
 * Representa una funcion con posibilidad de arrastre para las funciones
36
 * raices cuadradas y cuadradas. Con el raton se puede pasar de una a otra
37
 * directamente.
38
 * 
39
 * Las formas mas logicas de uso seria pasandole:
40
 * 1.0: Para una funcion raiz cuadrada
41
 * -1.0: Para una funcion cuadrada
42
 * 0.0: Para una funcion lineal
43
 * 
44
 * @author BorSanZa - Borja S?nchez Zamorano 
45
 */
46
@SuppressWarnings("unchecked")
47
public class DefaultSquareRootPowLine extends DefaultStraightLine implements SquareRootPowLine {
48
        /**
49
         * Numero de puntos que contiene esta funci?n
50
         */
51
        private double num = 40.0;
52
        
53
        private double valueFunction = 1.0;
54
        
55
        /**
56
         * Constructor. Asigna el color y establece la posicion de la funcion.
57
         * Los valores normales son 1.0 para raiz cuadrada y -1.0 para cuadrada.
58
         * El rango va desde -2.0 hasta 2.0. Siendo 0.0 una funcion lineal.
59
         * @param c
60
         */
61
        public DefaultSquareRootPowLine(Color c, double point) {
62
                super(c);
63
                setShowSquares(false);
64
                valueFunction = point;
65
                recalcList();
66
        }
67

    
68
        /**
69
         * Actualiza la barra informativa para saber en que estado se encuentra el
70
         * componente.
71
         * Cuando el porcentaje es mayor a 0 siempre estamos en la raiz cuadrada
72
         * Cuando el porcentaje es menor a 0 siempre estamos en la cuadrada
73
         * @param perc
74
         */
75
        private void setInfoPoint(Double perc) {
76
                if (canvas != null) {
77
                        List<DrawableElement> list = canvas.getDrawableElements(DefaultInfoLayer.class);
78
                        if (list.size() > 0) {
79
                                DefaultInfoLayer infoLayer = (DefaultInfoLayer) list.get(0);
80
                                
81
                                if (perc == null) {
82
                                        infoLayer.setStatusLeft(null);
83
                                        infoLayer.setStatusRight(null);
84
                                        ((Component)canvas).repaint();
85
                                        return;
86
                                }
87

    
88
                                if (perc.doubleValue() > 0.0)
89
                                        infoLayer.setStatusLeft(DefaultRasterSwingManager.getText(this, "square_root"));
90
                                else
91
                                        infoLayer.setStatusLeft(DefaultRasterSwingManager.getText(this, "pow"));
92

    
93
                                infoLayer.setStatusRight(clipDecimals(Math.abs(perc.doubleValue() * 100.0), 2) + "%");
94
                        }
95
                }
96
        }
97
        
98
        /**
99
         * Recalcula todos los puntos de la funcion
100
         * Posibles rangos para perc:
101
         * ( 0.0 a  1.0) - Funcion raiz cuadrada con aproximacion al centro
102
         * ( 1.0 a  2.0) - Funcion raiz cuadrada con aproximacion al borde
103
         * ( 0.0 a -1.0) - Funcion cuadrada con aproximacion al centro
104
         * (-1.0 a -2.0) - Funcion cuadrada con aproximacion al borde
105
         * 
106
         * @param perc
107
         */
108
        private void recalcList() {
109
                double x, y = 0.0;
110

    
111
                setInfoPoint(new Double(valueFunction));
112

    
113
                this.listSquare.clear();
114

    
115
                for (int i = 0; i <= num; i++) {
116
                        x = ((double) i) / num;
117

    
118
                        // Aproximacion al centro de una funcion raiz cuadrada
119
                        if (valueFunction >= 0.0 && valueFunction <= 1.0) {
120
                                y = squareFunction(x);
121
                                y = x + ((y - x) * valueFunction);
122
                        }
123

    
124
                        // Aproximacion al borde de una funcion raiz cuadrada
125
                        if (valueFunction > 1.0 && valueFunction <= 2.0) {
126
                                y = squareFunction(x);
127
                                y = y + ((1.0 - y) * (valueFunction - 1.0));
128
                        }
129

    
130
                        // Aproximacion al centro de una funcion cuadrada
131
                        if (valueFunction >= -1.0 && valueFunction < 0.0) {
132
                                y = powFunction(x);
133
                                y = x - ((x - y) * Math.abs(valueFunction));
134
                        }
135

    
136
                        // Aproximacion al borde de una funcion cuadrada
137
                        if (valueFunction >= -2.0 && valueFunction < -1.0) {
138
                                y = powFunction(x);
139
                                y = y * (1.0 - (Math.abs(valueFunction) - 1.0));
140
                        }
141

    
142
                        if (y < 0.0)
143
                                y = 0.0;
144
                        if (y > 1.0)
145
                                y = 1.0;
146

    
147
                        this.listSquare.add(new Square(x, y));
148
                }
149
        }
150
        
151
        /**
152
         * Formula para calcular el valor en y de una funcion raiz cuadrada en x
153
         * @param x
154
         * @return
155
         */
156
        private double squareFunction(double x) {
157
                return Math.sqrt(x);
158
        }
159
        
160
        /**
161
         * Formula para calcular el valor en y de una funcion cuadrada en x
162
         * @param x
163
         * @return
164
         */
165
        private double powFunction(double x) {
166
                return Math.pow(x, 2);
167
        }
168
        
169
        /*
170
         * (non-Javadoc)
171
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseDragged(java.awt.event.MouseEvent)
172
         */
173
        public boolean mouseDragged(MouseEvent e) {
174
                if (((Component)canvas).getCursor().getType() != Cursor.DEFAULT_CURSOR)
175
                        return true;
176

    
177
                double x = pixelToValueX(e.getX());
178
                double y = pixelToValueY(e.getY());
179
                
180
                double y2 = 0.0;
181
                
182
                // Localizamos el punto del raton para pasar un porcentaje correcto y que 
183
                // asi coincida la funcion con el raton
184
                if (y >= x) {
185
                        y2 = squareFunction(x);
186
                        if (y < y2)
187
                                valueFunction = (y - x) / (y2 - x);
188
                        else
189
                                valueFunction = ((y - y2) / (1.0 - y2)) + 1.0;
190
                } else {
191
                        y2 = powFunction(x);
192
                        
193
                        if (y > y2)
194
                                valueFunction = -Math.abs((y - x) / (y2 - x));
195
                        else
196
                                valueFunction = -Math.abs((y2 - y) / y2)  - 1.0;
197
                }
198
                
199
                if (valueFunction < -2.0)
200
                        valueFunction = -2.0;
201

    
202
                if (valueFunction > 2.0)
203
                        valueFunction = 2.0;
204

    
205
                recalcList();
206
                ((Component)canvas).repaint();
207
                canvas.callDataDragged("line", this);
208
                return false;
209
        }
210

    
211
        /*
212
         * (non-Javadoc)
213
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseMoved(java.awt.event.MouseEvent)
214
         */
215
        public boolean mouseMoved(MouseEvent e) {
216
                return true;
217
        }
218

    
219
        /*
220
         * (non-Javadoc)
221
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mousePressed(java.awt.event.MouseEvent)
222
         */
223
        public boolean mousePressed(MouseEvent e) {
224
                return mouseDragged(e);
225
        }
226

    
227
        /*
228
         * (non-Javadoc)
229
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#mouseReleased(java.awt.event.MouseEvent)
230
         */
231
        public boolean mouseReleased(MouseEvent e) {
232
                setInfoPoint(null);
233
                return true;
234
        }
235
        
236
        /*
237
         * (non-Javadoc)
238
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#getFunctionType()
239
         */
240
        public int getFunctionType() {
241
                return 2;
242
        }
243
        
244
        /*
245
         * (non-Javadoc)
246
         * @see org.gvsig.raster.beans.canvas.layers.functions.StraightLine#getValueFunction()
247
         */
248
        public double getValueFunction() {
249
                return valueFunction;
250
        }
251
}