Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrices in Objective-C

Is there any class for Matrix support in Objective-C? By Matrix I mean 2D-arrays.

What I do now is using 2 NSArrays, one within the other. This works perfectly but my code looks like a big mess.

I have also tried to use C-style arrays within each-other (matrix[][]) but this approach doesn't fit my application as I cannot automatically @synthesize or specify @properties for them.

I could of course create my own class for that, but what I'm wondering is if Objective-C already has something for this kind of situations. I did some Google-research but didn't find anything.

like image 696
Dimme Avatar asked Oct 10 '22 09:10

Dimme


2 Answers

Nope, Foundation doesn't have any 2D array class. As far as mathematical computations are concerned, Matrices are typically implemented in C or C++ for portability and performance reasons. You'll have to write your own class for that if you really want it.

like image 138
Matt Wilding Avatar answered Oct 12 '22 23:10

Matt Wilding


It seems obj-c has not its own struct for matrix. I refered to the iOS SDK's CATransform3D, found that it use:

struct CATransform3D
{
    CGFloat m11, m12, m13, m14;
    CGFloat m21, m22, m23, m24;
    CGFloat m31, m32, m33, m34;
    CGFloat m41, m42, m43, m44;
};

typedef struct CATransform3D CATransform3D;

as the 3D transform matrix.

like image 41
Kjuly Avatar answered Oct 13 '22 00:10

Kjuly