added Tree

This commit is contained in:
Louis Heredero 2024-04-24 12:56:31 +02:00
parent 11c90daaa2
commit 0e46e1a292
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7

36
src/logo/Tree.scala Normal file
View File

@ -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)
}
}