Hi there, I just tried running a Processing program written in the Kotlin language, and I thought I would share here the steps in case anyone wants to try.
This may not be very interesting if you are happy using the Processing IDE and your projects don’t grow very large. I wonder how many users in this forum use other editors like IntelliJ Idea… If you already do then you might be curious about trying this:
- Install IntelliJ Idea community edition (free), Processing and Kotlin.
- Create a new Kotlin/JVM project
- Go to Project Structure and add the
processing/core/library/
folder to Libraries - Create a new Kotlin file inside src/: Main.kt (or whatever you want to call it)
- Paste this example which is a slightly modified version of code by Donnie Flood
import processing.core.PApplet
import processing.core.PConstants
data class Circle(val radius: Float, val x: Float, val y: Float, var color: Int = 0)
class CirclePacking : PApplet() {
companion object Factory {
fun run() {
var art = CirclePacking()
art.setSize(500, 500)
art.runSketch()
}
}
private val circleSizeCounts = listOf(
65 to 19,
37 to 38,
20 to 75,
7 to 150,
3 to 300
)
private fun randomXY(xMin: Float, xMax: Float, yMin: Float, yMax: Float): Pair<Float, Float> {
return Pair(random(xMin, xMax), random(yMin, yMax))
}
override fun setup() {
colorMode(PConstants.HSB, 360f, 100f, 100f, 1.0f)
noStroke()
background(70)
val allCircles = mutableListOf<Circle>()
for (circleSizeCount in circleSizeCounts) {
val circleSize = circleSizeCount.first
val circleCount = circleSizeCount.second
for (i in 1..circleCount) {
// allow up to 100 collisions
for (c in 0..1000) {
// generate random point
// do not allow circles to overlap canvas
// val (x, y) = randomXY(0f+circleSize, 500f-circleSize, 0f+circleSize, 500f-circleSize);
// allow circles overlapping canvas
val (x, y) = randomXY(0f, width.toFloat(), 0f, height.toFloat());
val testCircle = Circle(circleSize.toFloat(), x, y)
if (!circleOverlaps(allCircles, testCircle)) {
// get random color
val c = weightedChoice(
listOf(
floatArrayOf(0f, 0f, random(90f, 100f)) to 0.6f,
floatArrayOf(random(180f, 220f), 50f, 50f) to 0.3f,
floatArrayOf(random(0f, 20f), 80f, 80f) to 0.1f
)
)
testCircle.color = color(c[0], c[1], c[2])
allCircles.add(testCircle)
break
}
}
}
}
for (circle in allCircles) {
fill(circle.color)
ellipse(circle.x, circle.y, circle.radius * 2, circle.radius * 2)
}
}
override fun draw() {
}
private fun circleOverlaps(allCircles: List<Circle>, testCircle: Circle): Boolean {
return allCircles.asSequence().any {
val distance = dist(it.x, it.y, testCircle.x, testCircle.y)
distance <= (it.radius + testCircle.radius)
}
}
private fun weightedChoice(colorsAndWeights: List<Pair<FloatArray, Float>>): FloatArray {
val weightSum = colorsAndWeights.sumBy { (it.second * 100).toInt() }
if (weightSum != 100) throw AssertionError("Weights should sum to 1")
val random = random(0f, 1.0f)
var weightTotal = 0f
for (i in colorsAndWeights) {
if (random >= weightTotal && random <= weightTotal + i.second) {
return i.first
}
weightTotal += i.second
}
throw Exception("Should have returned a Weighted Choice...")
}
}
fun main(args: Array<String>) {
CirclePacking.run()
}
- Right click on the Main.kt file on the left and choose Run (green triangle icon).
If everything is set up properly, the program should start after a few seconds.
In the code above you can see some similarities with a Java Processing program (import, class, PApplet, private, override
) and some differences (var, val, listOf
, no semicolons, types after and not before).
I do like how Kotlin code looks like. I’m concerned about it being young, and that it’s sponsored and developed by one company. On the other hand, it’s very active (multiple releases per day), has many contributors and many Android developers have switched to it.
The only mention to the word Kotlin in this forum was by @cansik in his presentation. What’s your experience with it @cansik? Has anyone else tried working with Kotlin? What are your thoughts on using this language for creative coding? Any advantages or disadvantages on using it? Is the effort of learning one more language worth it?