Files

134 lines
3.9 KiB
Java
Raw Permalink Normal View History

2021-10-14 17:07:54 +00:00
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2021-11-23 11:00:43 -08:00
// This file is automatically generated. Do not modify it.
2021-10-14 17:07:54 +00:00
package utils;
/** Utility methods for mathematical operations. */
2021-11-23 11:00:43 -08:00
public class MathUtils {
2021-10-14 17:07:54 +00:00
private MathUtils() {}
2021-11-23 11:00:43 -08:00
/**
* The signum function.
*
* @return 1 if num > 0, -1 if num < 0, and 0 if num = 0
*/
public static int signum(double num) {
if (num < 0) {
return -1;
2022-04-08 01:36:45 -07:00
} else if (num == 0) {
return 0;
2021-10-14 17:07:54 +00:00
} else {
2022-04-08 01:36:45 -07:00
return 1;
2021-10-14 17:07:54 +00:00
}
}
2021-11-23 11:00:43 -08:00
/**
* The linear interpolation function.
*
* @return start if amount = 0 and stop if amount = 1
*/
public static double lerp(double start, double stop, double amount) {
return (1.0 - amount) * start + amount * stop;
}
/**
* Clamps an integer between two integers.
*
* @return input when min <= input <= max, and either min or max otherwise.
*/
public static int clampInt(int min, int max, int input) {
if (input < min) {
return min;
2022-04-08 01:36:45 -07:00
} else if (input > max) {
return max;
2021-11-23 11:00:43 -08:00
}
2022-04-08 01:36:45 -07:00
2021-11-23 11:00:43 -08:00
return input;
}
/**
* Clamps an integer between two floating-point numbers.
*
* @return input when min <= input <= max, and either min or max otherwise.
*/
public static double clampDouble(double min, double max, double input) {
if (input < min) {
return min;
2022-04-08 01:36:45 -07:00
} else if (input > max) {
return max;
2021-11-23 11:00:43 -08:00
}
2022-04-08 01:36:45 -07:00
2021-11-23 11:00:43 -08:00
return input;
}
/**
* Sanitizes a degree measure as an integer.
*
* @return a degree measure between 0 (inclusive) and 360 (exclusive).
*/
public static int sanitizeDegreesInt(int degrees) {
degrees = degrees % 360;
2021-10-14 17:07:54 +00:00
if (degrees < 0) {
2021-11-23 11:00:43 -08:00
degrees = degrees + 360;
2021-10-14 17:07:54 +00:00
}
2021-11-23 11:00:43 -08:00
return degrees;
2021-10-14 17:07:54 +00:00
}
2021-11-23 11:00:43 -08:00
/**
* Sanitizes a degree measure as a floating-point number.
*
* @return a degree measure between 0.0 (inclusive) and 360.0 (exclusive).
*/
public static double sanitizeDegreesDouble(double degrees) {
degrees = degrees % 360.0;
if (degrees < 0) {
degrees = degrees + 360.0;
}
return degrees;
2021-10-14 17:07:54 +00:00
}
2022-05-02 10:19:54 -07:00
/**
* Sign of direction change needed to travel from one angle to another.
*
* <p>For angles that are 180 degrees apart from each other, both directions have the same travel
* distance, so either direction is shortest. The value 1.0 is returned in this case.
*
* @param from The angle travel starts from, in degrees.
* @param to The angle travel ends at, in degrees.
* @return -1 if decreasing from leads to the shortest travel distance, 1 if increasing from leads
* to the shortest travel distance.
*/
public static double rotationDirection(double from, double to) {
double increasingDifference = sanitizeDegreesDouble(to - from);
return increasingDifference <= 180.0 ? 1.0 : -1.0;
}
2021-11-23 11:00:43 -08:00
/** Distance of two points on a circle, represented using degrees. */
public static double differenceDegrees(double a, double b) {
return 180.0 - Math.abs(Math.abs(a - b) - 180.0);
}
/** Multiplies a 1x3 row vector with a 3x3 matrix. */
public static double[] matrixMultiply(double[] row, double[][] matrix) {
double a = row[0] * matrix[0][0] + row[1] * matrix[0][1] + row[2] * matrix[0][2];
double b = row[0] * matrix[1][0] + row[1] * matrix[1][1] + row[2] * matrix[1][2];
double c = row[0] * matrix[2][0] + row[1] * matrix[2][1] + row[2] * matrix[2][2];
return new double[] {a, b, c};
2021-10-14 17:07:54 +00:00
}
}
2021-11-23 11:00:43 -08:00