added NaturalTree

This commit is contained in:
Louis Heredero 2024-04-24 14:10:45 +02:00
parent 0e46e1a292
commit e6ec271afd
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
2 changed files with 100 additions and 2 deletions

View File

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

View File

@ -4,7 +4,7 @@ import hevs.graphics.TurtleGraphics
import java.awt.Point 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 = { def drawTree(n: Int, length: Double): Unit = {
if (n > 1) { if (n > 1) {
drawBranch(length) drawBranch(length)
@ -19,12 +19,17 @@ class Tree(width: Int, height: Int, val angleStep: Double) extends TurtleGraphic
drawTree(n - 1, length * 0.8) drawTree(n - 1, length * 0.8)
} else { } else {
drawBranch(length) drawBranch(length)
drawLeaf(length / 10)
} }
} }
def drawBranch(length: Double): Unit = { def drawBranch(length: Double): Unit = {
forward(length) forward(length)
} }
def drawLeaf(length: Double): Unit = {
}
} }
object Tree { object Tree {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {