My intention is to compare CGPoints
or CGPoint
values (and as the app is also for Mac OS NSPoints
or NSPoint
values) of several moving objects to detect if the objects have the same position.
My first solution to this was to fast enumerate an array of those objects and store all CGPoints
to an array, then fast enumerate the array of objects again to check whether the position is the same of any other object:
// STEP 1: Collect all Positions
NSMutableArray *allPositions = [NSMutableArray arrayWithCapacity:self.allObjects.count];
for (Object *myObject in self.allObjects) {
CGPoint myObjectPosition = ...;
[allPositions addObject:myObjectPosition]; // Problem here
}
// STEP 2: Check for Overlapping
for (Object *myObject in self.allObjects) {
CGPoint myObjectPosition = ...;
if ([allPositions containsObject:myObjectPosition] {
// Overlapping
}
}
The problem with this is adding the points to the allPositions
Array. Therefore NSValue
can be used:
[NSValue valueWithCGPoint:point];
But this does work only under iOS, for Mac OS there has to be used valueWithPoint
and NSPoint
.
Can I save maybe save the x
and the y
values in dictionaries and store them to the allPositions
Array? Or is there an even better solution without having 2x fast enumerations? There are about 100 objects in self.allObjects
...