comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/CrossSectionChartThemePanel.java @ 8468:67ae70259011

Cross section navigation: behave equally, regardless of whether an input value is below or above the range of possible values.
author Tom Gottfried <tom@intevation.de>
date Wed, 05 Nov 2014 19:07:49 +0100
parents 5fa1c8be43db
children 5e38e2924c07
comparison
equal deleted inserted replaced
8467:5fa1c8be43db 8468:67ae70259011
277 * the last one is taken. 277 * the last one is taken.
278 * @param in possible return values. 278 * @param in possible return values.
279 * @param to the value to be as close to as possible. 279 * @param to the value to be as close to as possible.
280 * @param up if true, prefer numerically higher values in case of two 280 * @param up if true, prefer numerically higher values in case of two
281 * values with equal distance to \param to. 281 * values with equal distance to \param to.
282 * @return value from in that is closest to to, -1 if none. 282 * @return value from in that is closest to to, NaN if none.
283 */ 283 */
284 public static Double closest(Double[] in, double to, boolean up) { 284 public static Double closest(Double[] in, double to, boolean up) {
285 if (in == null || in.length == 0) { 285 if (in == null || in.length == 0) {
286 return Double.NaN; 286 return Double.NaN;
287 } 287 }
288 Arrays.sort(in); 288 Arrays.sort(in);
289 GWT.log ("Closest match for " + (up ? "next" : "previous") + " value to: " + 289 GWT.log ("Closest match for " + (up ? "next" : "previous") + " value to: " +
290 to + " candidates: " + Arrays.toString(in)); 290 to + " candidates: " + Arrays.toString(in));
291 if (up) { 291 if (up) {
292 // take the first one that is larger 292 double max = in[in.length - 1];
293 for (int i = 0; i < in.length; i++) { 293 for (int i = 0; i < in.length; i++) {
294 if (in[i] >= to) { 294 if (in[i] >= to || in[i] == max) {
295 return in[i]; 295 return in[i];
296 } 296 }
297 } 297 }
298 } else { 298 } else {
299 double min = in[0];
299 for (int i = in.length - 1; i >= 0; i--) { 300 for (int i = in.length - 1; i >= 0; i--) {
300 if (in[i] <= to) { 301 if (in[i] <= to || in[i] == min) {
301 return in[i]; 302 return in[i];
302 } 303 }
303 } 304 }
304 } 305 }
305 GWT.log("Failed to find closest match"); 306 GWT.log("Failed to find closest match");

http://dive4elements.wald.intevation.org