All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.data2viz.shape.LineBuilder.kt Maven / Gradle / Ivy

There is a newer version: 0.8.0-RC5
Show newest version
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
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy