Search code examples
objective-carraysloopsnsdictionarycgpath

Create multiple CGPath in a loop


I am creating a program containing a map, in order to check in which region a mouseDown is occurring I want to create a CGPath per region. For the moment I create this part :

int brestPoints[] = {755,751,684,765,655,851,586,905,607,940,718,898,716,954,741,960,743,938,775,925,761,872,767,815};

brest = CGPathCreateMutable();

for (int i = 0; i < sizeof(brestPoints); i=i+2) {
    if (i == 0) {
        CGPathMoveToPoint(brest, NULL, brestPoints[i], 2203-brestPoints[i+1]);
    } else {
        CGPathAddLineToPoint(brest, NULL, brestPoints[i], 2203-brestPoints[i+1]);
    }
}

CGPathCloseSubpath(brest);

My problem is that I have 75 CGPath to create and I would like to loop over multiple arrays to create them all at once.

The ideal solution for me would be to have a dictionary with an array for object and the name of the region for key. But I cannot put the arrays as object as it can only store objects. Rewriting all 75 sets of coordinates to contain only NSNumber would be fastidious.

Any idea ?

UPDATE

I think there may subsist a confusion about what I'm trying to do, I have 75 C arrays like that :

int brestPoints[] = {755,751,684,765,655,851,586,905,607,940,718,898,716,954,741,960,743,938,775,925,761,872,767,815};
int picardyPoints[] = {769, 884, 778, 919, 859, 959, 956, 894, 949, 864, 864, 899, 842, 889};

I would need some way to create (simply) one NSBezierPath or CGPath for each array. And still know which Path belongs to which array (each array corresponds to one region of my map).

So I though of putting all of the arrays into a NSDictionary that would have an array associated with the region name as key. That way I could loop over the dictionary to create all my paths and still get to know the region they belong to. But I can seem to find a way to do that as C arrays can be put inside a NSDictionary.


Solution

  • What I'd do: Put the C arrays into a C array if they aren't already. Put the paths into an CFArray with the same order. Then you can just match the indices.