From 37314cf9db17ad8074953ee5b627eec5c92684a5 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 30 Apr 2024 13:17:19 +0200 Subject: [PATCH] added Snowflake --- src/logo/Snowflake.scala | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/logo/Snowflake.scala diff --git a/src/logo/Snowflake.scala b/src/logo/Snowflake.scala new file mode 100644 index 0000000..5a27274 --- /dev/null +++ b/src/logo/Snowflake.scala @@ -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) + } +} \ No newline at end of file