2d Matrix

BlitzMax Forums/BlitzMax Beginners Area/2d Matrix

Could someone tell me about matrices... In 3d I know they are invaluable, but what about in 2d.. can they help me? or is it a waste of time to bother in 2d.

I'm really interested in Learing about them.
Can anyone point me in the right direction. or maybe have some sample code.

They are a must have if you are doing something with hierarchical transformations ... but otherwise: naw max2d has everything that is needed.

I'm not sure what you mean ... " hierarchical transformations "

I was thinking of doing Vector style graphics.

If you want your whole object to rotate 50 degrees clockwise and you then want all the child objects within that to rotate 20 degrees more, you have a nested situation, for which you need the matrix.

Hierarchy: Parent - Child relation of objects where changing parent transformations are processed/executed on the childs as well.

For vector style graphics it depends on how you do it. If you use mathematical vectors, you most likely will need to use matrices.
But you could use the cairo module as well and use true vector graphics instead (check the user mods board)

Could someone multiply these two matricies together and give me the result so I can compare it to my function.

4.5 , 6.7 , 8.9               7.2 , 9.5 , 7.1

3.2 , 7.5 , 5.2        X      4.2 , 6.5 , 7.9

1.2 , 8.5 , 9.4               8.3 , 3.7 , 6.2


Any help would be appreciated.

Regards,
Eric

Here is code to do it, a matrix class from the code archives...(in 3d math)

Type Matrix

	Field e11:Float = 0, e12:Float = 0, e13:Float = 0
	Field e21:Float = 0, e22:Float = 0, e23:Float = 0
	Field e31:Float = 0, e32:Float = 0, e33:Float = 0

	Function Create:Matrix(e11#=0,e12#=0,e13#=0,e21#=0,e22#=0,e23#=0,e31#=0,e32#=0,e33#=0)
		Local m:Matrix = New Matrix
		m.e11 = e11
		m.e12 = e12
		m.e13 = e13
		m.e21 = e21
		m.e22 = e22
		m.e23 = e23
		m.e31 = e31
		m.e32 = e32
		m.e33 = e33
		Return m
	End Function

	Method Det:Float()
		Return e11*e22*e33-e11*e32*e23+e21*e32*e13-e21*e12*e33+e31*e12*e23-e31*e22*e13
	End Method

	Method Transpose:Matrix()
		Return Matrix.Create(e11,e21,e31,e12,e22,e32,e13,e23,e33)
	End Method
		
	Method 	Inverse:Matrix()
		Local dd:Float = e11*e22*e33-e11*e32*e23+e21*e32*e13-e21*e12*e33+e31*e12*e23-e31*e22*e13
		If dd = 0 Then dd = 1
		Local a:Float = (e22*e33-e23*e32)/dd
		Local b:Float = -(e12*e33-e13*e32)/dd
		Local c:Float = (e12*e23-e13*e22)/dd
		Local d:Float = -(e21*e33-e23*e31)/dd
		Local e:Float = (e11*e33-e13*e31)/dd
		Local f:Float = -(e11*e23-e13*e21)/dd
		Local g:Float = (e21*e32-e22*e31)/dd
		Local h:Float = -(e11*e32-e12*e31)/dd
		Local i:Float = (e11*e22-e12*e21)/dd
		Return Matrix.Create(a,b,c,d,e,f,g,h,i)
	End Method
		
	Method Add:Matrix( m:Matrix )
		Local res:Matrix = New Matrix
		res.e11 = e11 + m.e11
		res.e12 = e12 + m.e12
		res.e13 = e13 + m.e13
		res.e21 = e21 + m.e21
		res.e22 = e22 + m.e22
		res.e23 = e23 + m.e23
		res.e31 = e31 + m.e31
		res.e32 = e32 + m.e32
		res.e33 = e33 + m.e33
		Return res
	End Method

	Method Sub:Matrix( m:Matrix )
		Local res:Matrix = New Matrix
		res.e11 = e11 - m.e11
		res.e12 = e12 - m.e12
		res.e13 = e13 - m.e13
		res.e21 = e21 - m.e21
		res.e22 = e22 - m.e22
		res.e23 = e23 - m.e23
		res.e31 = e31 - m.e31
		res.e32 = e32 - m.e32
		res.e33 = e33 - m.e33
		Return res
	End Method

	Method Mul:Matrix( m:Matrix )
		Local res:Matrix = New Matrix
		res.e11 = e11*m.e11 + e12*m.e21 + e13*m.e31
		res.e12 = e11*m.e12 + e12*m.e22 + e13*m.e32
		res.e13 = e11*m.e13 + e12*m.e23 + e13*m.e33
		res.e21 = e21*m.e11 + e22*m.e21 + e23*m.e31
		res.e22 = e21*m.e12 + e22*m.e22 + e23*m.e32
		res.e23 = e21*m.e13 + e22*m.e23 + e23*m.e33
		res.e31 = e31*m.e11 + e32*m.e21 + e33*m.e31
		res.e32 = e31*m.e12 + e32*m.e22 + e33*m.e32
		res.e33 = e31*m.e13 + e32*m.e23 + e33*m.e33
		Return res
	End Method

	Method MulS:Matrix( s:Float )
		Local res:Matrix = New Matrix
		res.e11 = e11 * s
		res.e12 = e12 * s
		res.e13 = e13 * s
		res.e21 = e21 * s
		res.e22 = e22 * s
		res.e23 = e23 * s
		res.e31 = e31 * s
		res.e32 = e32 * s
		res.e33 = e33 * s
		Return res
	End Method

	Method DivS:Matrix( s:Float )
		Local res:Matrix = New Matrix
		res.e11 = e11 * s
		res.e12 = e12 * s
		res.e13 = e13 * s
		res.e21 = e21 * s
		res.e22 = e22 * s
		res.e23 = e23 * s
		res.e31 = e31 * s
		res.e32 = e32 * s
		res.e33 = e33 * s
		Return res
	End Method

	' Purpose: Multiply a Matrix by a Vector and Return a Vector Result
	' Example: v1=m1*v1 would be v1=m1.MulV( v1 )
	Method MulV:Vector( v:Vector )
		Local res:Vector = New Vector
		res.x = e11*v.x + e12*v.y + e13*v.z
		res.y = e21*v.x + e22*v.y + e23*v.z
		res.z = e31*v.x + e32*v.y + e33*v.z
		Return res
	End Method

End Type



edit:

Here is the two matrixs multiplied together with the above code:
134.409988 119.229996 140.059998
97.6999969 98.3899994 114.209999
122.360001 101.430000 133.949997

Here is the link to the full matrix/vector/quaternion classes:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1550