介绍:
我注册鼠标事件(mouseDragged、mouseDown)并在一些更正后将它们的位置转换为 NSValue。之后它们被添加到 NSMutableArray 中,稍后它们被取回,转换回 CGPoint,添加到 GLFloat(顶点数组)并通过 OpenGL 绘制。但是当鼠标拖动完成的速度比objective-c 注册事件的速度快时,点之间会出现很大的间距。我需要纠正这一点。
更多关于我的代码:
鼠标事件位置点注册后正在编辑中,从他那里得到4个点,稍后绘制glQuad。它看起来像这样:
locationPoint = [self convertPoint: [event locationInWindow] fromView:self];
//Top left corner of quad
quadTopLeftCorner.x = locationPoint.x - someFloatValue;
quadTopLeftCorner.y = locationPoint.y + someFloatValue;
//Top right corner of quad
quadTopRightCorner.x = locationPoint.x + someFloatValue;
quadTopRightCorner.y = locationPoint.y + someFloatValue;
//Bottom left corner of quad
quadBottomLeftCorner.x = locationPoint.x - someFloatValue;
quadBottomLeftCorner.y = locationPoint.y - someFloatValue;
//Bottom right corner of quad
quadBottomRightCorner.x = locationPoint.x + someFloatValue;
quadBottomRightCorner.y = locationPoint.y - someFloatValue;
他们正在被转换为 NSValue 并移动到 NSMutableArray
someNSValueTopLeft = [NSValue valueWithPoint:quadTopLeftCorner];
someNSValueTopRight = [NSValue valueWithPoint:quadTopRightCorner];
someNSValueBottomLeft = [NSValue valueWithPoint:quadBottomLeftCorner];
someNSValueBottomRight = [NSValue valueWithPoint:quadBottomRightCorner];
[someNSMutableArray addObject:someNSValueTopLeft];
[someNSMutableArray addObject:someNSValueTopRight];
[someNSMutableArray addObject:someNSValueBottomLeft];
[someNSMutableArray addObject:someNSValueBottomRight];
我想检查最后两点之间的距离是否大于某物。如果是的话,我想在它们之间添加一些要点。所以我试着这样做:
NSValue *someNSValueForLastNSValueTopLeft = [someNSMutableArray objectAtIndex:([someNSMutableArray count]-4)];
CGPoint lastTopLeftQuadPoint = someNSValueForLastNSValueTopLeft.pointValue;
int distx = (quadTopLeftCorner.x - lastTopLeftQuadPoint.x) * (quadTopLeftCorner.x - lastTopLeftQuadPoint.x);
int disty = (quadTopLeftCorner.y - lastTopLeftQuadPoint.y) * (quadTopLeftCorner.y - lastTopLeftQuadPoint.y);
int dist = sqrtf(distx + disty);
if (dist > someFloatValue) {
someNewPointForTopLeft.x = ((quadTopLeftCorner.x + lastTopLeftQuadPoint.x)/2);
someNewPointForTopLeft.y = ((quadTopLeftCorner.y + lastTopLeftQuadPoint.y)/2);
someNewPointForTopRight.x = someNewPointForTopLeft.x + someFloatValue;
someNewPointForTopRight.y = someNewPointForTopLeft.y;
someNewPointForBottomRight.x = someNewPointForTopLeft.x + someFloatValue;
someNewPointForBottomRight.y = someNewPointForTopLeft.y - someFloatValue;
someNewPointForBottomLeft.x = someNewPointForTopLeft.x;
someNewPointForBottomLeft.y = someNewPointForTopLeft.y - someFloatValue;
someNSValueTopLeft = [NSValue valueWithPoint:someNewPointForTopLeft];
[vertices addObject:someNSValueTopLeft];
someNSValueTopRight = [NSValue valueWithPoint:someNewPointForTopRight];
[vertices addObject:someNSValueTopRight];
someNSValueBottomLeft = [NSValue valueWithPoint:someNewPointForBottomRight];
[vertices addObject:someNSValueBottomLeft];
someNSValueBottomRight = [NSValue valueWithPoint:someNewPointForBottomLeft];
[vertices addObject:someNSValueBottomRight];
}
但它不起作用。任何人都可以建议我一些不同的方式?
结果: