Skip to content

Instantly share code, notes, and snippets.

@fiskurgit
Last active Dec 23, 2018
Embed
What would you like to do?
import org.openrndr.PresentationMode
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.math.Vector2
import kotlin.math.PI
import kotlin.random.Random
import org.openrndr.draw.Drawer
var angleVarianzPIDivider = 3f
var radius = 170f
var endLength = Random.nextInt(1, 3)
var reduce = Random.nextDouble(1.5, 2.5)
var randAngle = 0f
var single = true
var randomBrasnchAngles = false
fun main() = application {
program {
window.presentationMode = PresentationMode.MANUAL
}
configure {
width = 750
height = 750
title = "Sparsno"
}
program {
mouse.clicked.listen {
window.requestDraw()
}
extend {
angleVarianzPIDivider = 3f
radius = 170f //radius of the snowflake
endLength = Random.nextInt(1, 3)
reduce = Random.nextDouble(1.5, 2.5)
randAngle = 0f
single = true
randomBrasnchAngles = false
drawer.background(ColorRGBa.WHITE)
drawer.stroke = ColorRGBa.fromHex(0x55000000)
drawer.strokeWeight = 0.5
if (Random.nextInt(100) > 50) {
randomBrasnchAngles = true
}
for (i in 0 until 6) {
val origin = Vector2((width/2).toDouble(), (height/2).toDouble())
var angle = (360/6.0) * i
angle += (PI*2)/12
generateBranch(drawer, origin, radius.toDouble(), angle)
}
}
}
}
fun generateBranch(drawer: Drawer, origin: Vector2, bLength: Double, angle: Double) {
if (bLength < endLength) {
return
}
var randomAngle = Random.nextDouble(0.0, 360.0 / angleVarianzPIDivider)
if (!randomBrasnchAngles) {
randomAngle = 360.0/12
}
drawer.pushTransforms()
drawer.translate(origin)
drawer.rotate(angle)
drawer.lineSegment(0.0, 0.0, bLength, 0.0)
generateBranch(drawer, Vector2(0.0, 0.0), bLength / reduce, 0.0)
generateBranch(drawer, Vector2(bLength, 0.0), bLength / reduce, 0.0)
generateBranch(drawer, Vector2(bLength / reduce, 0.0), bLength / reduce, -randomAngle)
generateBranch(drawer, Vector2(bLength / reduce, 0.0), bLength / reduce, randomAngle)
drawer.popTransforms()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment