Class MathUtil

  1 /*
  2  * Copyright (c) 2016 Martin Davis.
  3  *
  4  * All rights reserved. This program and the accompanying materials
  5  * are made available under the terms of the Eclipse Public License 2.0
  6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  8  * and the Eclipse Distribution License is available at
  9  *
 10  * http://www.eclipse.org/org/documents/edl-v10.php.
 11  */
 12 package org.locationtech.jts.math;
 13  
 14 /**
 15  * Various utility functions for mathematical and numerical operations.
 16  * 
 17  * @author mbdavis
 18  *
 19  */
 20 public class MathUtil 
 21 {
 22   /**
 23    * Clamps a <tt>double</tt> value to a given range.
 24    * @param x the value to clamp
 25    * @param min the minimum value of the range
 26    * @param max the maximum value of the range
 27    * @return the clamped value
 28    */
 29   public static double clamp(double x, double min, double max)
 30   {
 31     if (x < min) return min;
 32     if (x > max) return max;
 33     return x;
 34   }
 35   
 36   /**
 37    * Clamps an <tt>int</tt> value to a given range.
 38    * @param x the value to clamp
 39    * @param min the minimum value of the range
 40    * @param max the maximum value of the range
 41    * @return the clamped value
 42    */
 43   public static int clamp(int x, int min, int max)
 44   {
 45     if (x < min) return min;
 46     if (x > max) return max;
 47     return x;
 48   }
 49   
 50   private static final double LOG_10 = Math.log(10);
 51   
 52   /**
 53    * Computes the base-10 logarithm of a <tt>double</tt> value.
 54    * <ul>
 55    * <li>If the argument is NaN or less than zero, then the result is NaN.
 56    * <li>If the argument is positive infinity, then the result is positive infinity.
 57    * <li>If the argument is positive zero or negative zero, then the result is negative infinity.
 58    * </ul>
 59    *   
 60    * @param x a positive number
 61    * @return the value log a, the base-10 logarithm of the input value
 62    */
 63   public static double log10(double x)
 64   {
 65     double ln = Math.log(x);
 66     if (Double.isInfinite(ln)) return ln;
 67     if (Double.isNaN(ln)) return ln;
 68     return ln / LOG_10;
 69   }
 70   
 71   /**
 72    * Computes an index which wraps around a given maximum value.
 73    * For values >= 0, this is equals to <tt>val % max</tt>.
 74    * For values < 0, this is equal to <tt>max - (-val) % max</tt>
 75    * 
 76    * @param index the value to wrap
 77    * @param max the maximum value (or modulus)
 78    * @return the wrapped index
 79    */
 80   public static int wrap(int index, int max)
 81   {
 82     if (index < 0) {
 83       return max - ((-index) % max);
 84     }
 85     return index % max;
 86   }
 87  
 88   /**
 89    * Computes the average of two numbers.
 90    * 
 91    * @param x1 a number
 92    * @param x2 a number
 93    * @return the average of the inputs
 94    */
 95   public static double average(double x1, double x2)
 96   {
 97     return (x1 + x2) / 2.0;
 98   }
 99   
100   public static double max(double v1, double v2, double v3)
101   {
102     double max = v1;
103     if (v2 > max) max = v2;
104     if (v3 > max) max = v3;
105     return max;
106   }
107   
108   public static double max(double v1, double v2, double v3, double v4)
109   {
110     double max = v1;
111     if (v2 > max) max = v2;
112     if (v3 > max) max = v3;
113     if (v4 > max) max = v4;
114     return max;
115   }
116   
117   public static double min(double v1, double v2, double v3, double v4)
118   {
119     double min = v1;
120     if (v2 < min) min = v2;
121     if (v3 < min) min = v3;
122     if (v4 < min) min = v4;
123     return min;
124   }
125 }
126