Search code examples
objective-cmultidimensional-arraycgpoint

How to create a multidimensional CGPoint array?


I want to have a 6 by 6 multidimensional array of CGpoints which will hold the vertex locations of 6 horizontal and 6 vertical lines. How would i do this?


Solution

  • If you want to use NSArrays to do this, it would look something like the following:

    NSMutableArray* topLevelArray = [NSMutableArray array];
    for(int i = 0; i < 6; i++){
        NSMutableArray* innerArray = [NSMutableArray array];
        [topLevelArray addObject:innerArray];
        for(int j = 0; j < 6; j++){
            CGPoint point = CGPointMake(x, y);
            [innerArray addObject:[NSValue valueWithCGPoint:point]];
        }
    }
    

    Then, to access a point from your array:

    CGPoint point = [topLevelArray[i][j] CGPointValue];