Created
October 7, 2010 03:08
-
-
Save kxbmap/614488 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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