Skip to content

Commit

Permalink
Move 2x2 and 4x4 matrices into their own modules
Browse files Browse the repository at this point in the history
  • Loading branch information
aardappel committed Sep 18, 2023
1 parent 3077e2c commit 3e7595f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 36 deletions.
33 changes: 33 additions & 0 deletions modules/mat2x2.lobster
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import vec

// A 2x2 matrix representated as a 4 length vector of x,y,z,w so that
// |x y|
// |z w|

struct mat2x2 : float4

def operator*(o:mat2x2):
return mat2x2 { dot(o.xz, this), dot(o.yw, this),
dot(o.xz, this.zw), dot(o.yw, this.zw) }

def operator*(o:float2):
return float2 { dot(o, this),
dot(o, this.zw) }

def determinant():
return x*w - y*z

def adjugate():
return mat2x2 { w, -y, -z, x }

def inverse():
return adjugate() / determinant()

def trace():
return x*w


// The identity matrix:
// |1 0|
// |0 1|
let mat2x2_identity = mat2x2 { 1.0, 0.0, 0.0, 1.0 }
35 changes: 1 addition & 34 deletions modules/matrix.lobster → modules/mat4x4.lobster
Original file line number Diff line number Diff line change
@@ -1,39 +1,6 @@
import vec

// A 2x2 matrix representated as a 4 length vector of x,y,z,w so that
// |x y|
// |z w|

struct mat2x2 : float4

def operator*(o:mat2x2):
return mat2x2 { dot(o.xz, this), dot(o.yw, this),
dot(o.xz, this.zw), dot(o.yw, this.zw) }

def operator*(o:float2):
return float2 { dot(o, this),
dot(o, this.zw) }

def determinant():
return x*w - y*z

def adjugate():
return mat2x2 { w, -y, -z, x }

def inverse():
return adjugate() / determinant()

def trace():
return x*w


// The identity matrix:
// |1 0|
// |0 1|
let mat2x2_identity = mat2x2 { 1.0, 0.0, 0.0, 1.0 }


// Unlike a smaller matrix like the one above, the 4x4 one is intended to be used with the gl. and matrix. functions,
// Unlike a smaller mat2x2, the 4x4 one is intended to be used with the gl. and matrix. functions,
// so stores the elements as a vector. Even though a [float] can support other matrix dimensions, all these
// built-in functions currently work with exactly 16 elements, hence the name.

Expand Down
2 changes: 1 addition & 1 deletion tests/matrixtest.lobster
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import testing
import matrix
import mat2x2

run_test("matrix"):

Expand Down
1 change: 0 additions & 1 deletion tests/structtest.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ run_test("struct"):
// With indexing as parent.
let v = [ int2_0 ]
v[0].y = 1
print v[0]
assert v[0] == int2_y

// Control structures.
Expand Down

0 comments on commit 3e7595f

Please sign in to comment.