import Math._
import java.awt.Color._
clear
invisible
val width = 180
val height = 25
val halfwave = Vector.tabulate(width){ i => toRadians(i) }
def render (level: Int)(fn: Double => Double) {
val y0 = level * height
val y1 = (level + 1) * height
(halfwave map fn zipWithIndex) foreach { case(e, i) =>
val c = e.toFloat
Canvas.setPenColor(new Color(c, c, c))
Canvas.line(i, y0, i, y1)
}
}
val f = { x: Double => 1 - x }
render (3) { sin }
render (2) { sin _ andThen f }
render (1) { cos _ andThen abs }
render (0) { cos _ andThen abs andThen { x => 1 - x } }
Very cool! And very functional!
It's almost scary to see someone using the Canvas API to such good effect - I have not had a chance to put in too much work at all into the implementation of that API.
If you identify anything that you consider to be pretty useful that is missing from the API, do let me know.
Will do —- I went through the Processing examples last autumn, porting them to Scala to evaluate the environment. I intend to see if I can use those ports in Kojo.
The last line should of course be
render (0) { cos _ andThen abs andThen f }
BTW, I registered an account and logged in, but I post as a guest. Is there something else I must to to post as a user, or is there just a delay?
BTW, I registered an account and logged in, but I post as a guest. Is there something else I must to to post as a
user, or is there just a delay?
You should be able to post as a registered (wikidot) user. When you're logged in, and you try to post something, you should not see fields such as name, email id etc on the posting page (those show up for anonymous users only).
Try again (maybe after restarting your browser), and let's see if it works out for you…
Ah. I registered with Kogics, not Wikidot. Silly me. :)