0

介绍:

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

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


结果:

在此处输入图像描述

4

1 回答 1

0

效果如何?

我只是注意到有两个错误:

  • 计算距离
  • 四边形的尺寸

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

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

希望这有帮助。

于 2012-08-07T09:45:16.060 回答