-
Notifications
You must be signed in to change notification settings - Fork 35
/
Util.java
85 lines (70 loc) · 2.37 KB
/
Util.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.team254.lib.util;
import com.team254.lib.geometry.Rotation2d;
import java.util.List;
/**
* Contains basic functions that are used often.
*/
public class Util {
public static final double kEpsilon = 1e-12;
/**
* Prevent this class from being instantiated.
*/
private Util() {}
/**
* Limits the given input to the given magnitude.
*/
public static double limit(double v, double maxMagnitude) {
return limit(v, -maxMagnitude, maxMagnitude);
}
public static double limit(double v, double min, double max) {
return Math.min(max, Math.max(min, v));
}
public static boolean inRange(double v, double maxMagnitude) {
return inRange(v, -maxMagnitude, maxMagnitude);
}
/**
* Checks if the given input is within the range (min, max), both exclusive.
*/
public static boolean inRange(double v, double min, double max) {
return v > min && v < max;
}
public static double interpolate(double a, double b, double x) {
x = limit(x, 0.0, 1.0);
return a + (b - a) * x;
}
public static String joinStrings(final String delim, final List<?> strings) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strings.size(); ++i) {
sb.append(strings.get(i).toString());
if (i < strings.size() - 1) {
sb.append(delim);
}
}
return sb.toString();
}
public static boolean epsilonEquals(double a, double b, double epsilon) {
return (a - epsilon <= b) && (a + epsilon >= b);
}
public static boolean epsilonEquals(double a, double b) {
return epsilonEquals(a, b, kEpsilon);
}
public static boolean epsilonEquals(int a, int b, int epsilon) {
return (a - epsilon <= b) && (a + epsilon >= b);
}
public static boolean allCloseTo(final List<Double> list, double value, double epsilon) {
boolean result = true;
for (Double value_in : list) {
result &= epsilonEquals(value_in, value, epsilon);
}
return result;
}
public static double toTurretSafeAngleDegrees(Rotation2d rotation2d) {
double result = rotation2d.getDegrees() % 360.0;
if (result > 270) {
result -= 360;
} else if (result < -90) {
result += 360;
}
return result;
}
}