-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathAbstractWave.java
More file actions
90 lines (73 loc) · 2.13 KB
/
AbstractWave.java
File metadata and controls
90 lines (73 loc) · 2.13 KB
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
86
87
88
89
90
package nodebox.util.waves;
public abstract class AbstractWave {
public static final float PI = 3.14159265358979323846f;
public static final float TWO_PI = 2 * PI;
public static enum Type { SINE, TRIANGLE, SQUARE, SAWTOOTH };
private float period;
private float phase;
private float frequency;
protected float amplitude;
private float offset;
public AbstractWave(float period, float amplitude) {
this(period, 0f, amplitude, 0);
}
public AbstractWave(float period, float phase, float amplitude) {
this(period, phase, amplitude, 0);
}
public AbstractWave(float period, float phase, float amplitude, float offset) {
this.period = period;
this.frequency = TWO_PI / period;
this.amplitude = amplitude;
this.offset = offset;
setPhase(phase);
}
public float getPeriod() {
return period;
}
public float getFrequency() {
return frequency;
}
public float getAmplitude() {
return amplitude;
}
public float getOffset() {
return offset;
}
public float getPhase() {
return phase;
}
/**
* (Re)sets the starting position of the wave.
*
* @param phase the new starting phase
*/
public void setPhase(float phase) {
phase %= TWO_PI;
if (phase < 0) phase += TWO_PI;
this.phase = phase;
}
/**
* Calculates and returns the value at time unit t for the wave.
*
* @param t time coordinate on the wave continuum
* @return the new value
*/
public float getValueAt(float t) {
float time = adjustedTime(t);
float phase;
if (time % period == 0)
phase = this.phase;
else
phase = (this.phase + time * frequency) % TWO_PI;
if (phase < 0) phase += TWO_PI;
return computeValue(phase) + offset;
}
protected abstract float adjustedTime(float t);
/**
* Calculates and returns the value at phase for the wave.
*
* @param phase
* @return the new value
*/
protected abstract float computeValue(float phase);
}