/** * file: Htree.kt * date: 2019-06-23 * * @author: SS * * Starting from n = 1, this program loops forever displaying * the level-n Htree, waits for the user to hit any key, then * displays the level-((n+1)%14) Htree, and waits for a key hit * by the user, and so on, ad infinitum. */ import processing.core.PApplet fun main(args: Array) { PApplet.main("HtreeSketch"); } class HtreeSketch : PApplet() { var level = 0 override fun settings() { //size(640, 640) fullScreen() noLoop() } override fun draw() { background(250) vsplit(width / 2F, height / 2F, width.toFloat(), height.toFloat(), ++level) level %= 14 } override fun keyPressed() { redraw() } fun vsplit(x: Float, y: Float, w: Float, h: Float, n: Int) { if (n < 1) return val x0 = x - w / 4F val x1 = x + w / 4F line(x0, y, x1, y) hsplit(x0, y, w / 2F, h, n - 1) hsplit(x1, y, w / 2F, h, n - 1) } fun hsplit(x: Float, y: Float, w: Float, h: Float, n: Int) { if (n < 1) return val y0 = y - h / 4F val y1 = y + h / 4F line(x, y0, x, y1) vsplit(x, y0, w, h / 2F, n - 1) vsplit(x, y1, w, h / 2F, n - 1) } }