commonMain.Line.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shape-jvm Show documentation
Show all versions of shape-jvm Show documentation
A collection of drawing/charting utilities
The newest version!
package com.juul.krayon.shape
import com.juul.krayon.kanvas.Path
import com.juul.krayon.shape.curve.Curve
import com.juul.krayon.shape.curve.Linear
private val DEFAULT_DEFINED = { _: Arguments<*> -> true }
private val DEFAULT_XY = { (datum): Arguments<*> -> (datum as? Number)?.toFloat() ?: 0f }
public fun line(): Line = Line()
public class Line internal constructor() : Shape {
private var curve: Curve = Linear
private var defined: (Arguments) -> Boolean = DEFAULT_DEFINED
private var x: (Arguments) -> Float = DEFAULT_XY
private var y: (Arguments) -> Float = DEFAULT_XY
public fun curve(curve: Curve): Line = this.apply { this.curve = curve }
public fun defined(defined: (Arguments) -> Boolean): Line = this.apply { this.defined = defined }
public fun x(x: Float): Line = this.apply { this.x = { x } }
public fun x(x: (Arguments) -> Float): Line = this.apply { this.x = x }
public fun y(y: Float): Line = this.apply { this.y = { y } }
public fun y(y: (Arguments) -> Float): Line = this.apply { this.y = y }
override fun render(data: List): Path = Path {
var currentlyDefined = false
val arguments = Arguments(Unit, -1, data)
for ((index, datum) in data.withIndex()) {
if (datum == null) {
if (currentlyDefined) {
curve.endLine(this)
currentlyDefined = false
}
continue
}
arguments.datum = datum
arguments.index = index
@Suppress("UNCHECKED_CAST")
arguments as Arguments
if (defined(arguments) != currentlyDefined) {
if (currentlyDefined) {
curve.endLine(this)
} else {
curve.startLine(this)
}
currentlyDefined = !currentlyDefined
}
if (currentlyDefined) {
curve.point(this, x(arguments), y(arguments))
}
}
}
}