Подтвердить что ты не робот

CGPoint для NSValue и наоборот

У меня есть код:

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

//Getting mouse coordinates
loc = [self convertPoint: [event locationInWindow] fromView:self];
[vertices addObject:loc]; // Adding coordinates to NSMutableArray

//Converting from NSMutableArray to GLfloat to work with OpenGL
int count = [vertices count] * 2; // * 2 for the two coordinates of a loc object
GLFloat []glVertices = (GLFloat *)malloc(count * sizeof(GLFloat));
int currIndex = 0;
for (YourLocObject *loc in vertices) {
    glVertices[currIndex++] = loc.x;
    glVertices[currIndex++] = loc.y;        
}

loc - это CGPoint, поэтому мне нужно как-то переключиться с CGPoint на NSValue, чтобы добавить его в NSMutableArray и после этого перевести его обратно в CGPoint. Как это можно сделать?

4b9b3361

Ответ 1

Класс NSValue имеет методы +[valueWithPoint:] и -[CGPointValue]? Это то, что вы ищете?

//Getting mouse coordinates
NSMutableArray *vertices = [[NSMutableArray alloc] init];
CGPoint location = [self convertPoint:event.locationInWindow fromView:self];
NSValue *locationValue = [NSValue valueWithPoint:location];
[vertices addObject:locationValue];

//Converting from NSMutableArray to GLFloat to work with OpenGL
NSUInteger count = vertices.count * 2; // * 2 for the two coordinates
GLFloat GLVertices[] = (GLFloat *)malloc(count * sizeof(GLFloat));
for (NSUInteger i = 0; i < count; i++) {
    NSValue *locationValue = [vertices objectAtIndex:i];
    CGPoint location = locationValue.CGPointValue;
    GLVertices[i] = location.x;
    GLVertices[i] = location.y;
}