Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit e543e92

Browse files
committed
cleanup on Cycles.kt
1 parent 6af8e07 commit e543e92

File tree

1 file changed

+22
-4
lines changed
  • desktop/interpolationEngines/src/main/kotlin/curves

1 file changed

+22
-4
lines changed

desktop/interpolationEngines/src/main/kotlin/curves/Cycles.kt

+22-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import kotlin.math.sin
2424
/**
2525
* This generates variable frequency oscillation curves
2626
*
27-
*
2827
*/
2928
class Cycles {
3029
private var mPeriod = floatArrayOf()
@@ -48,7 +47,14 @@ class Cycles {
4847
}
4948
}
5049

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+
*/
5258
fun addPoint(position: Float, period: Float) {
5359
val len = mPeriod.size + 1
5460
var j = Arrays.binarySearch(mPosition, position)
@@ -66,6 +72,7 @@ class Cycles {
6672

6773
/**
6874
* After adding point every thing must be normalized
75+
* This must be called adding points
6976
*/
7077
fun normalize() {
7178
var totalArea = 0f
@@ -91,6 +98,10 @@ class Cycles {
9198
mNormalized = true
9299
}
93100

101+
/**
102+
* Calculate the phase of the cycle given the accumulation of cycles up to
103+
* that point in time.
104+
*/
94105
private fun getP(time: Float): Float {
95106
var time = time
96107
if (time < 0) {
@@ -113,7 +124,11 @@ class Cycles {
113124
return p
114125
}
115126

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+
*/
117132
fun getValue(time: Float, phase: Float): Float {
118133
val angle = phase + getP(time) // angle is / by 360
119134
return when (mType) {
@@ -122,7 +137,7 @@ class Cycles {
122137
TRIANGLE_WAVE -> 1 - abs((angle * 4 + 1) % 4 - 2)
123138
SAW_WAVE -> (angle * 2 + 1) % 2 - 1
124139
REVERSE_SAW_WAVE -> 1 - (angle * 2 + 1) % 2
125-
COS_WAVE -> cos(mPI2 * (phase + angle))
140+
COS_WAVE -> cos(mPI2 * (angle))
126141
BOUNCE -> {
127142
val x = 1 - Math.abs(angle * 4 % 4 - 2)
128143
1 - x * x
@@ -133,6 +148,9 @@ class Cycles {
133148
}
134149
}
135150

151+
/**
152+
* Get the differential dValue/dt
153+
*/
136154
fun getDP(time: Float): Float {
137155
var time = time
138156
if (time <= 0) {

0 commit comments

Comments
 (0)