2009-11-05

Processing Basics

I've been working on a few Scala/Processing projects to get the hang of it, and today I started translating some of the basic examples to Scala just to make sure I was getting it.  This one is the Basics/Array2D example:

size(200, 200)
background(0)
val maxDistance: Float = dist(width/2, height/2, width, height)
val distances = Vector.tabulate(width, height){ case (i, j) =>
  dist(width/2, height/2, j, i)/maxDistance * 255
}

for (i <- 0 until(height, 2) ; j <- 0 until(width, 2)) {
  stroke(distances(j)(i))
  point(j, i)
}

To me, that's a lot better than the original Java.  Less clutter, more higher-level constructs.  Even in this simple example, the Scala code does a better job of showing the intentions rather than the specifics of the algorithm.

2 comments:

  1. hmmmm ... strangely arousing.

    ; )

    ReplyDelete
  2. Well, yes.


    size.
    (it doesn't matter, except when it matters.)

    maximum distance, width, height

    case of I and J (Joo? You?) leads to...

    width, height, width/2, height/2, wi--
    0 until height, 0 until width, nothing left outside

    stroke; distances of J and I

    point: teleia: convergence: meeting: merging: climax: setting one pixel alight

    J and I


    But seriously, code *is* somewhat arousing. ;)

    (The output of this particular code can be seen here (SFW): http://processing.org/learning/basics/array2d.html)

    ReplyDelete