Skip to content

Instantly share code, notes, and snippets.

@ochrons
Forked from anonymous/SierpinskiTriangle.scala
Last active April 14, 2018 14:13
Show Gist options
  • Save ochrons/3dfc003dedd4da5d821d to your computer and use it in GitHub Desktop.
Save ochrons/3dfc003dedd4da5d821d to your computer and use it in GitHub Desktop.
ScalaFiddle gist
import math._
val (h, w) = (Page.canvas.height, Page.canvas.width)
var x = 0.0
// $FiddleStart
val graphs = Seq[(String, Double => Double)](
("red", sin),
("green", x => 2 - abs(x % 8 - 4)),
("blue", x => 3 * pow(sin(x / 12), 2) * sin(x))
)
// $FiddleEnd
.zipWithIndex
val count = graphs.size
dom.window.setInterval(() => {
x = (x + 1) % w
if (x == 0) Page.renderer.clearRect(0, 0, w, h)
else for (((color, func), i) <- graphs) {
val y = func(x/w * 75) * h/40 + h/count * (i+0.5)
Page.renderer.fillStyle = color
Page.renderer.fillRect(x, y, 3, 3)
}
}, 10)
// $Template default
case class Pt(x: Double, y: Double)
val corners = Seq(
Pt(Page.canvas.width/2, 0),
Pt(0, Page.canvas.height),
Pt(Page.canvas.width, Page.canvas.height)
)
var p = corners(0)
val (w, h) = (Page.canvas.height.toDouble, Page.canvas.height.toDouble)
dom.window.setInterval(() => for(_ <- 0 until 10){
val c = corners(util.Random.nextInt(3))
p = Pt((p.x + c.x) / 2, (p.y + c.y) / 2)
val m = (p.y / h)
val r = 255 - (p.x / w * m * 255).toInt
val g = 255 - ((w-p.x) / w * m * 255).toInt
val b = 255 - ((h - p.y) / h * 255).toInt
Page.renderer.fillStyle = s"rgb($r, $g, $b)"
Page.renderer.fillRect(p.x, p.y, 1, 1)
}, 10)
println("Hello world!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment