Render performance

import io.data2viz.color.* import io.data2viz.geom.* import io.data2viz.math.* import io.data2viz.viz.* import io.data2viz.random.* import kotlin.math.sin import kotlin.math.cos private val vizSize = 600.0 private val circles = 1000 private val randomPosition = RandomDistribution.uniform(.0, vizSize) private val randomSpeed = RandomDistribution.uniform(-2.0, 2.0) private val positions = (0 .. circles).map { Point(randomPosition(), randomPosition()) }.toMutableList() private val speeds = (0 .. circles).map { Point(randomSpeed(), randomSpeed()) }.toMutableList() private fun updatePositions() = positions.forEachIndexed { index, position -> val newPos = position + speeds[index] positions[index] = newPos if (newPos.x < .0 || newPos.x > vizSize) { speeds[index] = Point(-speeds[index].x, speeds[index].y) } if (newPos.y < .0 || newPos.y > vizSize) { speeds[index] = Point(speeds[index].x, -speeds[index].y) } } fun main() { viz { size = size(vizSize, vizSize) val elements = positions.map { position -> circle { x = position.x y = position.y radius = 5.0 fill = Colors.Web.red } } animation { updatePositions() elements.forEachIndexed { index, element -> element.x = positions[index].x element.y = positions[index].y } } }.bindRendererOnNewCanvas() }
pierre avatar

Sketch created by

pierre