In gaming applications it would be very handy to have a 2-dimensional (or 3-dimensional) data structure.

Of course when I talk about a Matrix I don't mean the mathematical variant, therefore you can find much more performant implementations; but for the more common things like cell's on a board, I made my own Matrix class.

This first version is a very simple one, the underlying idea is to have an array of arrays. So a NxM matrix is implemented as a an array with N elements where each element is again an array of M elements.

Of course everything is kept simple now, in a latter version I'll more functions (like row access, column acces, transpose, copy, better initialization etc) and I'll add more complex things like SparseMatrix, Vectors, MatrixEnumerator etc.

I'll show now some code snippets , the full version can be downloaded here.

1. Matrix initialization

storage = [NSMutableArray arrayWithCapacity: j ] ;

for ( x = 0 ; x<=j ; x++ )

{

[storage insertObject: [NSMutableArray arrayWithCapacity: i ] atIndex: x] ;

for ( y = 0 ; y <= i ; y++)

{

// initialize each element with something

[[storage objectAtIndex: x]

insertObject: @"" atIndex: y ] ;

}

}

In the above snippet you see the initialization of a Matrix of size IxJ , as underlying datastructure I'll use the NSMutableArray.

Note that this initialization is oke for small matrices, and is usefull to be used in applications like chess or checkers.

2. Accessors for Matrix

Unfortunealy we don't have operator overloading like we have in C++ , but anyway. To access the elements of a matrix we have 2 accessors (a specialized get and set so to say).

- (void) atX: (int ) i atY: (int ) j put: (id) obj

{

//...

// store an element in the storage room

[[storage objectAtIndex: j]

replaceObjectAtIndex: i withObject: obj] ;

}

- (id) atX: (int ) i atY: (int ) j

{

//...

// get an element from the storage room

return [[storage objectAtIndex: j] objectAtIndex: i] ;

}

Now so far a basic matrix structure. Next time I'll go for some more advanced functions and structures.

Of course everything is kept simple now, in a latter version I'll more functions (like row access, column acces, transpose, copy, better initialization etc) and I'll add more complex things like SparseMatrix, Vectors, MatrixEnumerator etc.

I'll show now some code snippets , the full version can be downloaded here.

1. Matrix initialization

storage = [NSMutableArray arrayWithCapacity: j ] ;

for ( x = 0 ; x<=j ; x++ )

{

[storage insertObject: [NSMutableArray arrayWithCapacity: i ] atIndex: x] ;

for ( y = 0 ; y <= i ; y++)

{

// initialize each element with something

[[storage objectAtIndex: x]

insertObject: @"" atIndex: y ] ;

}

}

In the above snippet you see the initialization of a Matrix of size IxJ , as underlying datastructure I'll use the NSMutableArray.

Note that this initialization is oke for small matrices, and is usefull to be used in applications like chess or checkers.

2. Accessors for Matrix

Unfortunealy we don't have operator overloading like we have in C++ , but anyway. To access the elements of a matrix we have 2 accessors (a specialized get and set so to say).

- (void) atX: (int ) i atY: (int ) j put: (id) obj

{

//...

// store an element in the storage room

[[storage objectAtIndex: j]

replaceObjectAtIndex: i withObject: obj] ;

}

- (id) atX: (int ) i atY: (int ) j

{

//...

// get an element from the storage room

return [[storage objectAtIndex: j] objectAtIndex: i] ;

}

Now so far a basic matrix structure. Next time I'll go for some more advanced functions and structures.

## No comments:

## Post a Comment