added Snowflake

This commit is contained in:
Louis Heredero 2024-04-30 13:17:19 +02:00
parent e6ec271afd
commit 37314cf9db
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7

47
src/logo/Snowflake.scala Normal file
View File

@ -0,0 +1,47 @@
package logo
import hevs.graphics.TurtleGraphics
import java.awt.Point
class Snowflake(width: Int, height: Int) extends TurtleGraphics(width, height) {
def drawFlakeSegment(n: Int, length: Double): Unit = {
if (n == 1) {
forward(length)
return
}
val third: Double = length / 3
drawFlakeSegment(n - 1, third)
turn(-60)
drawFlakeSegment(n - 1, third)
turn(120)
drawFlakeSegment(n - 1, third)
turn(-60)
drawFlakeSegment(n - 1, third)
}
def drawFlake(n: Int, length: Double): Unit = {
val pos: Point = getPosition()
val x: Double = pos.x - length / 2
val y: Double = pos.y - math.sqrt(3) * length / 6
jump(x.toInt, y.toInt)
setAngle(0)
drawFlakeSegment(n, length)
turn(120)
drawFlakeSegment(n, length)
turn(120)
drawFlakeSegment(n, length)
turn(120)
}
}
object Snowflake {
def main(args: Array[String]): Unit = {
val win = new Snowflake(600, 600)
win.penUp()
win.jump(300, 300)
win.penDown()
win.drawFlake(5, 400)
}
}