MathUtils.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // This file is automatically generated. Do not modify it.
  17. package utils;
  18. /** Utility methods for mathematical operations. */
  19. public class MathUtils {
  20. private MathUtils() {}
  21. /**
  22. * The signum function.
  23. *
  24. * @return 1 if num > 0, -1 if num < 0, and 0 if num = 0
  25. */
  26. public static int signum(double num) {
  27. if (num < 0) {
  28. return -1;
  29. } else if (num == 0) {
  30. return 0;
  31. } else {
  32. return 1;
  33. }
  34. }
  35. /**
  36. * The linear interpolation function.
  37. *
  38. * @return start if amount = 0 and stop if amount = 1
  39. */
  40. public static double lerp(double start, double stop, double amount) {
  41. return (1.0 - amount) * start + amount * stop;
  42. }
  43. /**
  44. * Clamps an integer between two integers.
  45. *
  46. * @return input when min <= input <= max, and either min or max otherwise.
  47. */
  48. public static int clampInt(int min, int max, int input) {
  49. if (input < min) {
  50. return min;
  51. } else if (input > max) {
  52. return max;
  53. }
  54. return input;
  55. }
  56. /**
  57. * Clamps an integer between two floating-point numbers.
  58. *
  59. * @return input when min <= input <= max, and either min or max otherwise.
  60. */
  61. public static double clampDouble(double min, double max, double input) {
  62. if (input < min) {
  63. return min;
  64. } else if (input > max) {
  65. return max;
  66. }
  67. return input;
  68. }
  69. /**
  70. * Sanitizes a degree measure as an integer.
  71. *
  72. * @return a degree measure between 0 (inclusive) and 360 (exclusive).
  73. */
  74. public static int sanitizeDegreesInt(int degrees) {
  75. degrees = degrees % 360;
  76. if (degrees < 0) {
  77. degrees = degrees + 360;
  78. }
  79. return degrees;
  80. }
  81. /**
  82. * Sanitizes a degree measure as a floating-point number.
  83. *
  84. * @return a degree measure between 0.0 (inclusive) and 360.0 (exclusive).
  85. */
  86. public static double sanitizeDegreesDouble(double degrees) {
  87. degrees = degrees % 360.0;
  88. if (degrees < 0) {
  89. degrees = degrees + 360.0;
  90. }
  91. return degrees;
  92. }
  93. /**
  94. * Sign of direction change needed to travel from one angle to another.
  95. *
  96. * <p>For angles that are 180 degrees apart from each other, both directions have the same travel
  97. * distance, so either direction is shortest. The value 1.0 is returned in this case.
  98. *
  99. * @param from The angle travel starts from, in degrees.
  100. * @param to The angle travel ends at, in degrees.
  101. * @return -1 if decreasing from leads to the shortest travel distance, 1 if increasing from leads
  102. * to the shortest travel distance.
  103. */
  104. public static double rotationDirection(double from, double to) {
  105. double increasingDifference = sanitizeDegreesDouble(to - from);
  106. return increasingDifference <= 180.0 ? 1.0 : -1.0;
  107. }
  108. /** Distance of two points on a circle, represented using degrees. */
  109. public static double differenceDegrees(double a, double b) {
  110. return 180.0 - Math.abs(Math.abs(a - b) - 180.0);
  111. }
  112. /** Multiplies a 1x3 row vector with a 3x3 matrix. */
  113. public static double[] matrixMultiply(double[] row, double[][] matrix) {
  114. double a = row[0] * matrix[0][0] + row[1] * matrix[0][1] + row[2] * matrix[0][2];
  115. double b = row[0] * matrix[1][0] + row[1] * matrix[1][1] + row[2] * matrix[1][2];
  116. double c = row[0] * matrix[2][0] + row[1] * matrix[2][1] + row[2] * matrix[2][2];
  117. return new double[] {a, b, c};
  118. }
  119. }