added Tree
This commit is contained in:
parent
11c90daaa2
commit
0e46e1a292
36
src/logo/Tree.scala
Normal file
36
src/logo/Tree.scala
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user