added Snowflake
This commit is contained in:
parent
e6ec271afd
commit
37314cf9db
47
src/logo/Snowflake.scala
Normal file
47
src/logo/Snowflake.scala
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user