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