import io.data2viz.color.*
import io.data2viz.geom.*
import io.data2viz.math.*
import io.data2viz.timer.*
import io.data2viz.force.*
import io.data2viz.viz.*
import io.data2viz.random.*
fun main() {
val vizSize = 200.0
val randPos = RandomDistribution.uniform(.0, vizSize)
data class LayeredPoint(val position:Point, val layer:Int)
val items = (0..500).map { LayeredPoint(point(randPos(), randPos()), it%12 ) }
lateinit var viz:Viz
val viewCenter = point(vizSize / 2, vizSize / 2)
val simulation1 = forceSimulation<LayeredPoint> {
initForceNode = {
position = domain.position
}
forceRadial {
centerGet = { viewCenter }
radiusGet = { domain.layer * 7.0 }
}
domainObjects = items
intensityMin = 1.pct
intensityDecay = 1.pct
on(SimulationEvent.END, "End of simulation", { viz.stopAnimations() })
}
val simulation2 = forceSimulation<LayeredPoint> {
initForceNode = {
position = domain.position
}
forceRadial {
centerGet = { viewCenter }
radiusGet = { domain.layer * 7.0 }
}
domainObjects = items
intensityMin = 30.pct
intensityDecay = 1.pct
}
val simulation3 = forceSimulation<LayeredPoint> {
initForceNode = {
position = domain.position
}
forceRadial {
centerGet = { viewCenter }
radiusGet = { domain.layer * 7.0 }
}
domainObjects = items
intensityMin = 70.pct
intensityDecay = 1.pct
}
val particles1 = mutableListOf<CircleNode>()
val particles2 = mutableListOf<CircleNode>()
val particles3 = mutableListOf<CircleNode>()
viz = viz {
size = size(600, 200)
simulation1.nodes.forEach { forceNode ->
particles1 += circle {
radius = 2.0
fill = Colors.hsl((forceNode.domain.layer * 30).deg, 100.pct, 50.pct)
}
}
simulation2.nodes.forEach { forceNode ->
particles2 += circle {
radius = 2.0
fill = Colors.hsl((forceNode.domain.layer * 30).deg, 100.pct, 50.pct)
}
}
simulation3.nodes.forEach { forceNode ->
particles3 += circle {
radius = 2.0
fill = Colors.hsl((forceNode.domain.layer * 30).deg, 100.pct, 50.pct)
}
}
animation {
simulation1.nodes.forEach { forceNode ->
particles1[forceNode.index].apply {
x = forceNode.x
y = forceNode.y
}
}
simulation2.nodes.forEach { forceNode ->
particles2[forceNode.index].apply {
x = 200 + forceNode.x
y = forceNode.y
}
}
simulation3.nodes.forEach { forceNode ->
particles3[forceNode.index].apply {
x = 400 + forceNode.x
y = forceNode.y
}
}
}
}
viz.bindRendererOnNewCanvas()
}
comments