/
IntCurver.java
62 lines (52 loc) · 1.57 KB
/
IntCurver.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
package ch.epfl.sweng.SDP.game.drawing;
import java.util.Arrays;
import static ch.epfl.sweng.SDP.utils.Preconditions.checkPrecondition;
/**
* This class helps smoothing new cursor position values by remembering older values and returning
* an average.
*/
final class IntCurver {
private int index;
private int curveIntensity;
private int sum;
private int[] values;
/**
* This constructor initializes the array with a given size and fills it with a given value.
*
* @param curveIntensity amount of values that should be remembered
* @param initValue initial value of the curver
*/
IntCurver(int curveIntensity, int initValue) {
checkPrecondition(curveIntensity > 0, "CurveIntensity was not positive.");
this.curveIntensity = curveIntensity;
values = new int[curveIntensity];
Arrays.fill(values, initValue);
sum = curveIntensity * initValue;
index = 0;
}
/**
* Resets the values to a given point.
*
* @param value the value at which the existing values should be reset
*/
void setValue(int value) {
Arrays.fill(values, value);
sum = curveIntensity * value;
}
/**
* Adds a value to the list of entries.
*
* @param value the value to add
*/
void addValue(int value) {
sum += value - values[index];
values[index] = value;
index = (index + 1) % curveIntensity;
}
/**
* Returns the average of the stored values.
*/
int getValue() {
return sum / curveIntensity;
}
}