From e6ec271afd9378ca2daf2152608771873e7d2b3e Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 24 Apr 2024 14:10:45 +0200 Subject: [PATCH] added NaturalTree --- src/logo/NaturalTree.scala | 93 ++++++++++++++++++++++++++++++++++++++ src/logo/Tree.scala | 9 +++- 2 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 src/logo/NaturalTree.scala diff --git a/src/logo/NaturalTree.scala b/src/logo/NaturalTree.scala new file mode 100644 index 0000000..09d690d --- /dev/null +++ b/src/logo/NaturalTree.scala @@ -0,0 +1,93 @@ +package logo + +import java.awt.{Color, Point} +import scala.util.Random + +class NaturalTree(width: Int, height: Int, angleStep_ : Double) extends Tree(width, height, angleStep_) { + val MIN_STRAW_ANGLE: Double = 60 + val MAX_STRAW_ANGLE: Double = 100 + val MIN_N_STRAWS: Int = 90 + val MAX_N_STRAWS: Int = 200 + //val STRAW_COLOR1: Color = new Color(230, 188, 43) + //val STRAW_COLOR2: Color = new Color(255, 226, 71) + //val STRAW_COLOR1: Color = new Color(96, 163, 67) + //val STRAW_COLOR2: Color = new Color(112, 198, 76) + val STRAW_COLOR1: Color = new Color(108, 83, 61) + val STRAW_COLOR2: Color = new Color(174, 105, 44) + val TRUNK_COLOR: Color = new Color(65, 61, 64) + + override def drawTree(n: Int, length: Double): Unit = { + var n2: Int = n + val r: Double = math.random() + if (r < 0.25) { + n2 -= 1 + } else if (r > 0.9) { + if (n2 < 2) { + n2 += 3 + } + } + turn(angleStep * (math.random() - 0.5) * 2) + val a: Double = angleStep + angleStep -= math.random() * angleStep / 10 + super.drawTree(n2, length) + angleStep = a + } + + override def drawBranch(length: Double): Unit = { + setPenWidth((length / 10).toFloat) + setColor(TRUNK_COLOR) + super.drawBranch(length) + } + + override def drawLeaf(length: Double): Unit = { + val angle: Double = getTurtleAngle() + val pos: Point = getPosition() + val nStraws: Int = Random.between(MIN_N_STRAWS, MAX_N_STRAWS) + val strawsAngle: Double = Random.between(MIN_STRAW_ANGLE, MAX_STRAW_ANGLE) + val step: Double = strawsAngle / (nStraws - 1) + val oAngle: Double = angle - strawsAngle / 2 + val strawLen: Double = length * 20 + + setPenWidth(1f) + for (i: Int <- 0 until nStraws) { + setPenWidth(0.5f + (nStraws - i) / (nStraws - 1f) / 10) + setColor(lerpCol((nStraws - i) / (nStraws - 1.0))) + //val a: Double = oAngle + i * step + (math.random() * 2 -1) * step / 5 + val a: Double = oAngle + math.random() * strawsAngle + var l: Double = strawLen * (nStraws.toDouble - i) / nStraws + l += (math.random() * 2 - 1) * l / 2 + setAngle(a) + jump(pos.x, pos.y) + //forward(strawLen + (math.random() * 2 - 1) * strawLen / 2) + forward(l) + } + jump(pos.x, pos.y) + setAngle(angle) + } + + def lerpCol(f: Double): Color = { + val r: Int = ((STRAW_COLOR2.getRed - STRAW_COLOR1.getRed) * f + STRAW_COLOR1.getRed).toInt + val g: Int = ((STRAW_COLOR2.getGreen - STRAW_COLOR1.getGreen) * f + STRAW_COLOR1.getGreen).toInt + val b: Int = ((STRAW_COLOR2.getBlue - STRAW_COLOR1.getBlue) * f + STRAW_COLOR1.getBlue).toInt + return new Color( + math.min(255, math.max(0, r)), + math.min(255, math.max(0, g)), + math.min(255, math.max(0, b)) + ) + } +} + +object NaturalTree { + def main(args: Array[String]): Unit = { + val tree: NaturalTree = new NaturalTree(600, 600, 25) + tree.jump(300, 600) + tree.setAngle(-90) + tree.drawTree(8, 80) + + for (i: Int <- 0 until 10) { + tree.jump(Random.between(0, 600), 600) + tree.setAngle(-90) + tree.drawTree(3, 20) + } + } +} \ No newline at end of file diff --git a/src/logo/Tree.scala b/src/logo/Tree.scala index a1455b6..3599d3a 100644 --- a/src/logo/Tree.scala +++ b/src/logo/Tree.scala @@ -4,14 +4,14 @@ import hevs.graphics.TurtleGraphics import java.awt.Point -class Tree(width: Int, height: Int, val angleStep: Double) extends TurtleGraphics(width, height) { +class Tree(width: Int, height: Int, var 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 ) + turn(-angleStep) drawTree(n - 1, length * 0.8) jump(pos.x, pos.y) setAngle(angle) @@ -19,12 +19,17 @@ class Tree(width: Int, height: Int, val angleStep: Double) extends TurtleGraphic drawTree(n - 1, length * 0.8) } else { drawBranch(length) + drawLeaf(length / 10) } } def drawBranch(length: Double): Unit = { forward(length) } + + def drawLeaf(length: Double): Unit = { + + } } object Tree { def main(args: Array[String]): Unit = {