diff --git a/src/matrix.nim b/src/matrix.nim index 30cb819..2bdd4fa 100644 --- a/src/matrix.nim +++ b/src/matrix.nim @@ -1,5 +1,5 @@ import strformat -from "./tuple" import `=~` +from "./tuple" import `=~`, Tuple4, tuple4 type Matrix* = object @@ -10,6 +10,7 @@ type MatrixMultiplicationException = object of CatchableError IdentityMatrixCreationError = object of CatchableError MatrixInversionError = object of CatchableError + MatrixConversionError = object of CatchableError proc matrix(width, height: int): Matrix = result.matrix = newSeq[float](width * height) @@ -23,9 +24,19 @@ proc matrix*(data: seq[float], width, height: int): Matrix = result.width = width result.height = height +proc matrix*(input: Tuple4): Matrix = + result.matrix = @[input.x, input.y, input.z, input.w] + result.width = 1 + result.height = 4 + template `[]`*(mat: Matrix, row, column: int): float = mat.matrix[row * mat.width + column] template `[]=`*(mat: var Matrix, row, column: int, val: float) = mat.matrix[row * mat.width + column] = val +proc toTuple4(i: Matrix): Tuple4 = + if(i.width != 1 or i.height != 4): + raise newException(MatrixConversionError, "Matrix is of incorrect dimensions to convert to a Tuple4") + return tuple4(i[0,0], i[0,1], i[0,2], i[0,3]) + proc `==`*(lhs, rhs: Matrix): bool = if lhs.width != rhs.width or lhs.height != rhs.height: return false for i in 0..