1 /* 2 * Copyright 2021 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // This file is automatically generated. Do not modify it. 18 19 package com.google.ux.material.libmonet.utils; 20 21 /** Utility methods for mathematical operations. */ 22 public class MathUtils { MathUtils()23 private MathUtils() {} 24 25 /** 26 * The signum function. 27 * 28 * @return 1 if num > 0, -1 if num < 0, and 0 if num = 0 29 */ signum(double num)30 public static int signum(double num) { 31 if (num < 0) { 32 return -1; 33 } else if (num == 0) { 34 return 0; 35 } else { 36 return 1; 37 } 38 } 39 40 /** 41 * The linear interpolation function. 42 * 43 * @return start if amount = 0 and stop if amount = 1 44 */ lerp(double start, double stop, double amount)45 public static double lerp(double start, double stop, double amount) { 46 return (1.0 - amount) * start + amount * stop; 47 } 48 49 /** 50 * Clamps an integer between two integers. 51 * 52 * @return input when min <= input <= max, and either min or max otherwise. 53 */ clampInt(int min, int max, int input)54 public static int clampInt(int min, int max, int input) { 55 if (input < min) { 56 return min; 57 } else if (input > max) { 58 return max; 59 } 60 61 return input; 62 } 63 64 /** 65 * Clamps an integer between two floating-point numbers. 66 * 67 * @return input when min <= input <= max, and either min or max otherwise. 68 */ clampDouble(double min, double max, double input)69 public static double clampDouble(double min, double max, double input) { 70 if (input < min) { 71 return min; 72 } else if (input > max) { 73 return max; 74 } 75 76 return input; 77 } 78 79 /** 80 * Sanitizes a degree measure as an integer. 81 * 82 * @return a degree measure between 0 (inclusive) and 360 (exclusive). 83 */ sanitizeDegreesInt(int degrees)84 public static int sanitizeDegreesInt(int degrees) { 85 degrees = degrees % 360; 86 if (degrees < 0) { 87 degrees = degrees + 360; 88 } 89 return degrees; 90 } 91 92 /** 93 * Sanitizes a degree measure as a floating-point number. 94 * 95 * @return a degree measure between 0.0 (inclusive) and 360.0 (exclusive). 96 */ sanitizeDegreesDouble(double degrees)97 public static double sanitizeDegreesDouble(double degrees) { 98 degrees = degrees % 360.0; 99 if (degrees < 0) { 100 degrees = degrees + 360.0; 101 } 102 return degrees; 103 } 104 105 /** 106 * Sign of direction change needed to travel from one angle to another. 107 * 108 * <p>For angles that are 180 degrees apart from each other, both directions have the same travel 109 * distance, so either direction is shortest. The value 1.0 is returned in this case. 110 * 111 * @param from The angle travel starts from, in degrees. 112 * @param to The angle travel ends at, in degrees. 113 * @return -1 if decreasing from leads to the shortest travel distance, 1 if increasing from leads 114 * to the shortest travel distance. 115 */ rotationDirection(double from, double to)116 public static double rotationDirection(double from, double to) { 117 double increasingDifference = sanitizeDegreesDouble(to - from); 118 return increasingDifference <= 180.0 ? 1.0 : -1.0; 119 } 120 121 /** Distance of two points on a circle, represented using degrees. */ differenceDegrees(double a, double b)122 public static double differenceDegrees(double a, double b) { 123 return 180.0 - Math.abs(Math.abs(a - b) - 180.0); 124 } 125 126 /** Multiplies a 1x3 row vector with a 3x3 matrix. */ matrixMultiply(double[] row, double[][] matrix)127 public static double[] matrixMultiply(double[] row, double[][] matrix) { 128 double a = row[0] * matrix[0][0] + row[1] * matrix[0][1] + row[2] * matrix[0][2]; 129 double b = row[0] * matrix[1][0] + row[1] * matrix[1][1] + row[2] * matrix[1][2]; 130 double c = row[0] * matrix[2][0] + row[1] * matrix[2][1] + row[2] * matrix[2][2]; 131 return new double[] {a, b, c}; 132 } 133 } 134 135