forked from Team254/FRC-2019-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterpolatingDouble.java
47 lines (41 loc) · 1.26 KB
/
InterpolatingDouble.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
package com.team254.lib.util;
/**
* A Double that can be interpolated using the InterpolatingTreeMap.
*
* @see InterpolatingTreeMap
*/
public class InterpolatingDouble implements Interpolable<InterpolatingDouble>, InverseInterpolable<InterpolatingDouble>,
Comparable<InterpolatingDouble> {
public Double value = 0.0;
public InterpolatingDouble(Double val) {
value = val;
}
@Override
public InterpolatingDouble interpolate(InterpolatingDouble other, double x) {
Double dydx = other.value - value;
Double searchY = dydx * x + value;
return new InterpolatingDouble(searchY);
}
@Override
public double inverseInterpolate(InterpolatingDouble upper, InterpolatingDouble query) {
double upper_to_lower = upper.value - value;
if (upper_to_lower <= 0) {
return 0;
}
double query_to_lower = query.value - value;
if (query_to_lower <= 0) {
return 0;
}
return query_to_lower / upper_to_lower;
}
@Override
public int compareTo(InterpolatingDouble other) {
if (other.value < value) {
return 1;
} else if (other.value > value) {
return -1;
} else {
return 0;
}
}
}