From 0e46e1a2923b86e281ac34dc0ad32233bd082ffa Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 24 Apr 2024 12:56:31 +0200 Subject: [PATCH] added Tree --- src/logo/Tree.scala | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/logo/Tree.scala 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) + } +}