首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGPoint到NSValue和反转

CGPoint到NSValue和反转
EN

Stack Overflow用户
提问于 2012-07-04 18:27:32
回答 1查看 11.2K关注 0票数 12

我有代码:

代码语言:javascript
复制
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。怎么可能做到呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-04 18:30:30

NSValue有方法+[valueWithPoint:]-[CGPointValue]?这就是你要找的东西吗?

代码语言:javascript
复制
//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;
}
票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11327249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档