diff --git a/src/logo/Tree.scala b/src/logo/Tree.scala new file mode 100644 index 0000000..a1455b6 --- /dev/null +++ b/src/logo/Tree.scala @@ -0,0 +1,36 @@ +package logo + +import hevs.graphics.TurtleGraphics + +import java.awt.Point + +class Tree(width: Int, height: Int, val angleStep: Double) extends TurtleGraphics(width, height) { + def drawTree(n: Int, length: Double): Unit = { + if (n > 1) { + drawBranch(length) + val pos: Point = getPosition() + val angle: Double = getTurtleAngle() + + turn(-angleStep ) + drawTree(n - 1, length * 0.8) + jump(pos.x, pos.y) + setAngle(angle) + turn(angleStep) + drawTree(n - 1, length * 0.8) + } else { + drawBranch(length) + } + } + + def drawBranch(length: Double): Unit = { + forward(length) + } +} +object Tree { + def main(args: Array[String]): Unit = { + val tree: Tree = new Tree(400, 400, 15) + tree.jump(200, 400) + tree.setAngle(-90) + tree.drawTree(8, 60) + } +}