Skip to content

Instantly share code, notes, and snippets.

@kxbmap
Created October 7, 2010 03:08
Show Gist options
  • Save kxbmap/614488 to your computer and use it in GitHub Desktop.
Save kxbmap/614488 to your computer and use it in GitHub Desktop.
class Matrix extends Function2[Int, Int, Float] {
import Matrix.{ width, height }
private val e = new Array[Float](width * height)
def apply(row : Int, col : Int) = e(row * width + col)
def update(row : Int, col : Int, value : Float) { e(row * width + col) = value }
def initializeWith(f : (Int, Int) => Float) = {
for {
row <- 0 until height
val ofs = row * width
col <- 0 until width
} e(ofs + col) = f(row, col)
this
}
def identity() = initializeWith((r, c) => if (r == c) 1f else 0f)
// ...
}
object Matrix {
final val width = 4
final val height = 4
def apply() = new Matrix identity
def apply(m : Matrix) = new Matrix initializeWith m
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment