Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you store (x, y) coordinates in an array using Objective-C?

I have an Objective-C method which uses some x and y values from an image: image.center.x and image.center.y. I want to store them away every time this method is called, so I was hoping to use an array.

How can this be done? I suspect using an NSMutableArray?

like image 888
mac_newbie Avatar asked Nov 29 '22 19:11

mac_newbie


2 Answers

C arrays are a proper subset of Objective C, as well as producing faster code and often using less memory than using Cocoa Foundation classes. You could add:

CGPoint myPoints[MAX_NUMBER_OF_POINTS];

to your instance variables; and save coordinates with:

myPoints[i] = image.center;
like image 30
hotpaw2 Avatar answered Dec 06 '22 00:12

hotpaw2


I would recommend storing the points in an NSArray, wrapped using NSValue:

NSMutableArray *arrayOfPoints = [[NSMutableArray alloc] init];

[arrayOfPoints addObject:[NSValue valueWithCGPoint:image.center]];

// Do something with the array
[arrayOfPoints release];

This assumes that image.center is a CGPoint struct (if not, you can make one using CGPointMake()).

To extract the CGPoint, simply use

[[arrayOfPoints objectAtIndex:0] CGPointValue];
like image 107
Brad Larson Avatar answered Dec 06 '22 00:12

Brad Larson