io.data2viz.shape.LineBuilder.kt Maven / Gradle / Ivy
package io.data2viz.shape
import io.data2viz.geom.Path
fun line(init: LineBuilder.() -> Unit) = LineBuilder().apply(init)
fun const(constantValue: T): (D) -> T = { constantValue }
class LineBuilder {
var curve: (Path) -> Curve = curves.linear
var x: (T) -> Double = const(.0)
var y: (T) -> Double = const(.0)
var defined: (T) -> Boolean = const(true)
/**
* Use the data to generate a line on the path
*/
fun buildLine(data: List, path: C): C {
val dataSize = data.size
var defined0 = false
val output = curve(path)
for (i in 0..dataSize) {
if (!(i < dataSize && defined(data[i])) == defined0) {
defined0 = !defined0
if (defined0) output.lineStart() else output.lineEnd()
}
if (defined0) {
val d = data[i]
output.point(x(d), y(d))
}
}
return path
}
}