|
| 1 | +/* |
| 2 | + * This software Copyright by the RPTools.net development team, and |
| 3 | + * licensed under the Affero GPL Version 3 or, at your option, any later |
| 4 | + * version. |
| 5 | + * |
| 6 | + * MapTool Source Code is distributed in the hope that it will be |
| 7 | + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 8 | + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 9 | + * |
| 10 | + * You should have received a copy of the GNU Affero General Public |
| 11 | + * License * along with this source Code. If not, please visit |
| 12 | + * <http://www.gnu.org/licenses/> and specifically the Affero license |
| 13 | + * text at <http://www.gnu.org/licenses/agpl.html>. |
| 14 | + */ |
| 15 | +package net.rptools.lib; |
| 16 | + |
| 17 | +import org.apache.logging.log4j.LogManager; |
| 18 | +import org.apache.logging.log4j.Logger; |
| 19 | + |
| 20 | +/* Utility class for useful mathematical methods */ |
| 21 | +public class MathUtil { |
| 22 | + private static final Logger log = LogManager.getLogger(MathUtil.class); |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns a truncated double with the specified number of decimal places |
| 26 | + * |
| 27 | + * @param value to be truncated |
| 28 | + * @param decimalPlaces number of decimal places to use |
| 29 | + * @return truncated double value |
| 30 | + */ |
| 31 | + public static double doublePrecision(double value, int decimalPlaces) { |
| 32 | + double divisor = Math.pow(10, -decimalPlaces); |
| 33 | + return divisor * Math.round(value / divisor); |
| 34 | + } |
| 35 | + |
| 36 | + public static boolean isInt(Object o) { |
| 37 | + if (o == null) { |
| 38 | + return false; |
| 39 | + } else { |
| 40 | + return o.getClass().isAssignableFrom(Integer.class); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Checks that a value lies within a specified tolerance. Useful for checking if a value is "close |
| 46 | + * enough" |
| 47 | + * |
| 48 | + * @param checkValue to be checked |
| 49 | + * @param referenceValue to be checked against |
| 50 | + * @param tolerance variance allowed |
| 51 | + * @return true if the value is within ± tolerance |
| 52 | + */ |
| 53 | + public static boolean inTolerance(double checkValue, double referenceValue, double tolerance) { |
| 54 | + return checkValue <= referenceValue + tolerance && checkValue >= referenceValue - tolerance; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Maps a value in one range to its equivalent in a second range |
| 59 | + * |
| 60 | + * @param valueToMap value in the first range that needs to be converted |
| 61 | + * @param in_min the minimum value for the original range |
| 62 | + * @param in_max the maximum value for the original range |
| 63 | + * @param out_min the minimum value for the target range |
| 64 | + * @param out_max the maximum value for the target range |
| 65 | + * @return the equivalent value of valueToMap in the target range |
| 66 | + */ |
| 67 | + public static double mapToRange( |
| 68 | + double valueToMap, double in_min, double in_max, double out_min, double out_max) { |
| 69 | + return (valueToMap - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
| 70 | + } |
| 71 | +} |
0 commit comments