class RecursiveCallTree : Fragment() {
override val root = group {
val root1 = this
val start = Point(400.0, 600.0)
val level= intProperty()
val levelList= listOf(1,3,5,10,15,20)
hbox {
button("draw tree") {
action {
run {
drawTree(root1, start, 90.0, 10)
}
}
}
combobox(level,levelList)
}
}
fun drawTree(group: Group, p0: Point, angle: Double, level: Int) {
if (level <= 0) {
return
}
val destination = p0.getDestination(10F, angle, level)
val line = Line(p0.x, p0.y, destination.x, destination.y)
line.setStrokeWidth(level.toDouble())
if (level > 7) {
line.setStroke(Color.BROWN)
} else if (level > 3) {
line.setStroke(Color.GREEN)
} else {
line.setStroke(Color.LIMEGREEN)
}
group.add(line)
drawTree(group, destination, angle - 20.0 - Math.random() * 10, level - 1)
drawTree(group, destination, angle - 5, level - 1)
drawTree(group, destination, angle + 20.0 + Math.random() * 20, level - 1)
}
}
class Point(var x: Double=0.0,var y: Double=0.0) {
fun getDestination(distance: Float, angle: Double, level: Int): Point {
val p = Point()
p.x = x + Math.cos(Math.toRadians(angle)) * level.toDouble() * distance.toDouble()
p.y = y - Math.sin(Math.toRadians(angle)) * level.toDouble() * distance.toDouble()
return p
}
}