Sorting an array of vertex points X and Y? iOS / Goal C

I have a Core Data object called Line. Each row contains an instance of VerticePoint that contains the x and y properties. These x and y vertices form simple two-dimensional polygons.

What I want to do is sort the array of these Line objects, which is in random order, so that the beginning of the form, the bottom left point, is always the first element in the array, and then the rest of the wounds are counterclockwise from the origin.

So say the points in my original array (the xy axis is centered at 0,0):

x = 20, y = 20
x = 20 , y= 10
x = 10, y=10
x = 10, y =20
x = 15, y = 10

I want to sort them like this:

x = 10, y=10
x = 15, y = 10
x = 20 , y= 10
x = 20, y = 20
x = 10, y =20

Many thanks

+4
source share
3

- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors

NSArray.

. , x, y.

+1

:

  • , ( y).
  • .
  • . , , -, 0 ° .

:

NSArray *points = @[
    [NSValue valueWithCGPoint:(CGPoint){20, 20}],
    [NSValue valueWithCGPoint:(CGPoint){20, 10}],
    [NSValue valueWithCGPoint:(CGPoint){10, 10}],
    [NSValue valueWithCGPoint:(CGPoint){10, 20}],
    [NSValue valueWithCGPoint:(CGPoint){15, 10}],
];

CGPoint min = [points[0] CGPointValue];
CGPoint max = min;
for (NSValue *value in points) {
    CGPoint point = [value CGPointValue];
    min.x = fminf(point.x, min.x);
    min.y = fminf(point.y, min.y);
    max.x = fmaxf(point.x, max.x);
    max.y = fmaxf(point.y, max.y);
}

CGPoint center = {
    0.5f * (min.x + max.x),
    0.5f * (min.y + max.y),
};

NSLog(@"center: %@", NSStringFromCGPoint(center));

NSNumber *(^angleFromPoint)(id) = ^(NSValue *value){
    CGPoint point = [value CGPointValue];
    CGFloat theta = atan2f(point.y - center.y, point.x - center.x);
    CGFloat angle = fmodf(M_PI - M_PI_4 + theta, 2 * M_PI);
    return @(angle);
};

NSArray *sortedPoints = [points sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    return [angleFromPoint(a) compare:angleFromPoint(b)];
}];

NSLog(@"sorted points: %@", sortedPoints);
+13

VerticePoint, , - :

- (NSComparisonResult)compare:(VerticePoint *)vpoint
{
    if (self.x > vpiont.x)
        return NSOrderedAscending;
    else if (self.x < vpiont.x)
        return NSOrderedDescending;
    else if (self.y > vpiont.y)
        return NSOrderedAscending;
    else if (self.y < vpiont.y)
        return NSOrderedDescending;
    else 
        return NSOrderedSame;
}

, VerticePoint, :

NSArray *sortedArray = [yourArray sortedArrayUsingSelector:@selector(compare:)];

.

//EXTENDED

NSManagedObject, NSSortDescriptor:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"ENTITYNAME" inManagedObjectContext:context]];

NSSortDescriptor *sortDescriptorX = [NSSortDescriptor sortDescriptorWithKey:@"yourObjecy.x" ascending:YES];
NSSortDescriptor *sortDescriptorY = [NSSortDescriptor sortDescriptorWithKey:@"yourObjecy.y" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptorX, sortDescriptorY, nil]];

NSArray *sortedResults = [context executeFetchRequest:request error:nil];

//EXTENDED

-

NSArray *returnedVertices = [verticesPassed sortedArrayUsingComparator:^(id obj1, id obj2) {
    //Cast to your object:
    VerticePoint *p1 = (VerticePoint*)obj1;
    VerticePoint *p2 = (VerticePoint*)obj2;
    if (p1.x > p2.x)
        return NSOrderedAscending;
    else if (p1.x < p2.x)
        return NSOrderedDescending;
    else if (p1.y > p2.y)
        return NSOrderedAscending;
    else if (p1.y < p2.y)
        return NSOrderedDescending;
    else 
        return NSOrderedSame;
}

];

0
source

All Articles