[orx-delegate-magic] Add more magic
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
@file:Suppress("PackageDirectoryMismatch")
|
||||
|
||||
package org.openrndr.extra.delegatemagic.aggregation
|
||||
|
||||
import kotlin.math.abs
|
||||
|
||||
fun List<Double>.maxMag(): Double {
|
||||
if (isEmpty()) {
|
||||
error("list is empty")
|
||||
}
|
||||
var maxMag = Double.NEGATIVE_INFINITY
|
||||
var maxMagWithSign = 0.0
|
||||
|
||||
for (i in indices) {
|
||||
val a = abs(this[i])
|
||||
if (a > maxMag) {
|
||||
maxMag = a
|
||||
maxMagWithSign = this[i]
|
||||
}
|
||||
}
|
||||
return maxMagWithSign
|
||||
}
|
||||
|
||||
fun List<Double>.minMag(): Double {
|
||||
if (isEmpty()) {
|
||||
error("list is empty")
|
||||
}
|
||||
var minMag = Double.POSITIVE_INFINITY
|
||||
var minMagWithSign = 0.0
|
||||
|
||||
for (i in indices) {
|
||||
val a = abs(this[i])
|
||||
if (a < minMag) {
|
||||
minMag = a
|
||||
minMagWithSign = this[i]
|
||||
}
|
||||
}
|
||||
return minMagWithSign
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
@file:Suppress("PackageDirectoryMismatch")
|
||||
|
||||
package org.openrndr.extra.delegatemagic.aggregation
|
||||
|
||||
import org.openrndr.Clock
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.KProperty0
|
||||
|
||||
class ListPropertyAggregation<T, R>(
|
||||
private val clock: Clock,
|
||||
private val property: KProperty0<List<T>>,
|
||||
val aggregationFunction: (List<T>) -> R
|
||||
) {
|
||||
private var output: R? = null
|
||||
private var lastTime: Double? = null
|
||||
|
||||
operator fun getValue(any: Any?, property: KProperty<*>): R {
|
||||
if (lastTime != null) {
|
||||
val dt = clock.seconds - lastTime!!
|
||||
if (dt > 1E-10) {
|
||||
output = aggregationFunction(this.property.get())
|
||||
}
|
||||
} else {
|
||||
output = aggregationFunction(this.property.get())
|
||||
}
|
||||
|
||||
lastTime = clock.seconds
|
||||
return output!!
|
||||
}
|
||||
}
|
||||
|
||||
fun <T, R> Clock.aggregating(
|
||||
property: KProperty0<List<T>>,
|
||||
aggregationFunction: (List<T>) -> R
|
||||
): ListPropertyAggregation<T, R> {
|
||||
return ListPropertyAggregation(this, property, aggregationFunction)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package difference
|
||||
|
||||
import org.openrndr.Clock
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.KProperty0
|
||||
|
||||
class DoublePropertyDifferencer(
|
||||
private val clock: Clock,
|
||||
private val property: KProperty0<Double>,
|
||||
) {
|
||||
private var lastValue: Double? = null
|
||||
private var output: Double? = null
|
||||
private var lastTime: Double? = null
|
||||
operator fun getValue(any: Any?, property: KProperty<*>): Double {
|
||||
if (lastTime != null) {
|
||||
val dt = clock.seconds - lastTime!!
|
||||
if (dt > 1E-10) {
|
||||
output = this.property.get() - lastValue!!
|
||||
lastValue = this.property.get()
|
||||
}
|
||||
} else {
|
||||
lastValue = this.property.get()
|
||||
output = lastValue!! - lastValue!!
|
||||
}
|
||||
lastTime = clock.seconds
|
||||
return output ?: error("no value")
|
||||
}
|
||||
}
|
||||
|
||||
fun Clock.differencing(property: KProperty0<Double>) = DoublePropertyDifferencer(this, property)
|
||||
Reference in New Issue
Block a user