中间点

时间:2012-08-07 09:07:19

标签: objective-c opengl mouseevent

简介:

我注册鼠标事件(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];
    }

但它不起作用。有人可以建议我一些不同的方式吗?


结果:

enter image description here

1 个答案:

答案 0 :(得分:0)

有什么影响?

我注意到有两个错误:

  • 计算距离
  • quad的维度

距离代码有一些错误,试试这个:

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); 

如果你从左上角组成四边形,你还需要多次两次someFloatValue:

someNewPointForTopLeft.x = ((quadTopLeftCorner.x + lastTopLeftQuadPoint.x)/2);         
someNewPointForTopLeft.y = ((quadTopLeftCorner.y + lastTopLeftQuadPoint.y)/2); 

someNewPointForTopRight.x = someNewPointForTopLeft.x + someFloatValue*2;         
someNewPointForTopRight.y = someNewPointForTopLeft.y;         

someNewPointForBottomRight.x = someNewPointForTopLeft.x + someFloatValue*2;         
someNewPointForBottomRight.y = someNewPointForTopLeft.y - someFloatValue*2;  

someNewPointForBottomLeft.x = someNewPointForTopLeft.x;         
someNewPointForBottomLeft.y = someNewPointForTopLeft.y - someFloatValue*2;

无论如何,为了改进代码和未来的管理,我建议更改代码,引入一个Vector库并制作如下代码:

Vector a = lastPoint;
Vector b = currentPoint;
Vector newPointForQuad=lastPoint;

if ( (lastPoint-currentPoint).dist()> someDistanceValue) newPointForQuad = (lastPoint + currentPoint)/2;

Quad quad = makeQuadAroundPoint(newPointForQuad, someQuadSize);

希望得到这个帮助。