@@ -24,7 +24,6 @@ import kotlin.math.sin
24
24
/* *
25
25
* This generates variable frequency oscillation curves
26
26
*
27
- *
28
27
*/
29
28
class Cycles {
30
29
private var mPeriod = floatArrayOf()
@@ -48,7 +47,14 @@ class Cycles {
48
47
}
49
48
}
50
49
51
- // @TODO: add description
50
+ /* *
51
+ * This adds a point in the cycle positions are from 0..1
52
+ * period represents the number of oscillations.
53
+ * The periods should typically:
54
+ * - add up to a whole number.
55
+ * - have a value at 0 and 1.
56
+ * After all points are added call normalize
57
+ */
52
58
fun addPoint (position : Float , period : Float ) {
53
59
val len = mPeriod.size + 1
54
60
var j = Arrays .binarySearch(mPosition, position)
@@ -66,6 +72,7 @@ class Cycles {
66
72
67
73
/* *
68
74
* After adding point every thing must be normalized
75
+ * This must be called adding points
69
76
*/
70
77
fun normalize () {
71
78
var totalArea = 0f
@@ -91,6 +98,10 @@ class Cycles {
91
98
mNormalized = true
92
99
}
93
100
101
+ /* *
102
+ * Calculate the phase of the cycle given the accumulation of cycles up to
103
+ * that point in time.
104
+ */
94
105
private fun getP (time : Float ): Float {
95
106
var time = time
96
107
if (time < 0 ) {
@@ -113,7 +124,11 @@ class Cycles {
113
124
return p
114
125
}
115
126
116
- // @TODO: add description
127
+ /* *
128
+ * Get the value for time. (Time here is typically progress 0..1)
129
+ * Phase typically thought of as an angle is from 0..1 (not 0-360 or 0-2PI)
130
+ * This makes it mathematically more efficient
131
+ */
117
132
fun getValue (time : Float , phase : Float ): Float {
118
133
val angle = phase + getP(time) // angle is / by 360
119
134
return when (mType) {
@@ -122,7 +137,7 @@ class Cycles {
122
137
TRIANGLE_WAVE -> 1 - abs((angle * 4 + 1 ) % 4 - 2 )
123
138
SAW_WAVE -> (angle * 2 + 1 ) % 2 - 1
124
139
REVERSE_SAW_WAVE -> 1 - (angle * 2 + 1 ) % 2
125
- COS_WAVE -> cos(mPI2 * (phase + angle))
140
+ COS_WAVE -> cos(mPI2 * (angle))
126
141
BOUNCE -> {
127
142
val x = 1 - Math .abs(angle * 4 % 4 - 2 )
128
143
1 - x * x
@@ -133,6 +148,9 @@ class Cycles {
133
148
}
134
149
}
135
150
151
+ /* *
152
+ * Get the differential dValue/dt
153
+ */
136
154
fun getDP (time : Float ): Float {
137
155
var time = time
138
156
if (time <= 0 ) {
0 commit comments