import java.awt.Color._
def sq[A](x: A)(implicit n: Numeric[A]): A = n.times(x, x)
def dist[A](x0: A, y0: A, x1: A, y1: A)(implicit n: Numeric[A]): A = {
val v = n.plus(sq(n.minus(x0, x1)), sq(n.minus(y0, y1)))
val r = Math.sqrt(n.toDouble(v))
if (v.isInstanceOf[Int]) r.toInt.asInstanceOf[A]
else if (v.isInstanceOf[Float]) r.toFloat.asInstanceOf[A]
else r.asInstanceOf[A]
}
clear
invisible
Canvas.fgClear //?
val width = 200
val height = 200
// background(0)
val maxDistance = dist(width/2, height/2, width, height)
val distances = Seq.tabulate(width, height){ case (i, j) =>
dist(width/2f, height/2f, j, i)/maxDistance * 255
}
for (i <- 0 until(height, 2) ; j <- 0 until(width, 2)) {
val c = distances(j)(i).toInt
Canvas.setPenColor(new Color(c, c, c))
Canvas.point(j, i)
}
Another example, with sq and dist methods
Summary:
A straight port of the Array2D example from Processing. The most interesting part is the sq and dist methods, which should be generic enough (requires 2.8, I think) and possibly efficient (I'm not implementation-savvy in the Java environment).