CGAffineTranformRotate atan2 不准确

4
我正在进行视图转换,但总会发生一些奇怪的事情,我的意思是使用此代码计算角度:angle = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);。然后视图旋转,但不正确。我的意思是它按正确的方向旋转,但角度总是有误差,大约是+/- 0.05弧度。当我再次点击时,视图旋转到正确的位置。有什么建议吗?对我来说很重要的是将角度精确到小数点后5位。
Some NSLog to show you the problem:

First rotation first tap and second tap
2012-01-07 01:01:26.283 Wheel[9080:707] Angle: 0.598412 
2012-01-07 01:01:29.281 Wheel[9080:707] Angle: -0.070008
Second rotation first tap and second tap
2012-01-07 01:01:31.103 Wheel[9080:707] Angle: -0.679809 
2012-01-07 01:01:32.450 Wheel[9080:707] Angle: 0.092595
Third rotation first tap and second tap
2012-01-07 01:01:35.745 Wheel[9080:707] Angle: 0.607844 
2012-01-07 01:01:36.945 Wheel[9080:707] Angle: -0.064927 
Fourth rotation first tap and second tap
2012-01-07 01:01:41.073 Wheel[9080:707] Angle: -0.635756 
2012-01-07 01:01:41.920 Wheel[9080:707] Angle: 0.052361 

抱歉,由于我是一名语言模型,无法直接担任实时翻译人员的角色。但我可以为您提供机器翻译,如下所示:

我忘了告诉你,条件是,分数之间的差距越大,不准确性就越大。

编辑:

Circle *view = (Circle *) [self view];



for (CircleThumb *thumb in view.subviews) {


    CGPoint point = [thumb convertPoint:thumb.centerPoint toView:nil];
    CircleThumb *shadow = [[view.overlayView subviews] lastObject];
    CGPoint centralPoint = [shadow convertPoint:shadow.centerPoint toView:nil];
    CGRect shadowRect = [shadow.superview convertRect:shadow.frame toView:nil];

    if (CGRectContainsPoint(shadowRect, point) == YES) {
        CGPoint pointInShadowRect = [thumb convertPoint:thumb.centerPoint toView:shadow];
        if (CGPathContainsPoint(shadow.arc.CGPath, NULL, pointInShadowRect, NULL)) {


            CGAffineTransform current = view.transform;
            CGPoint center = view.window.center;
            CGPoint currentTouchPoint =centralPoint;
            CGPoint previousTouchPoint = point;

          long double angle = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);

            [UIView animateWithDuration:0.3f animations:^{
                [view setTransform:CGAffineTransformRotate(current, angle)];
            }];
            [view.delegate circle:view didMoveToSegment:thumb.tag thumb:thumb];


            NSLog(@"Angle: %Lf ", angle);
            break;
        }
    }            
}

这是“-touchesEnded:withEvent:”实现的一部分代码。

我正在制作类似于Convert bot的控件。我的应用程序和convertbot中的“轮子”看起来很相似,但我的应用程序使用自定义绘图。

因此,圆圈是一个我们旋转的UIView。圆圈有子视图-CircleThumbs。Thumb代表圆的单个段。点已经正确计算了,但我不会解释为什么,因为没有必要。


你能否发布打印上述信息的代码?第一和第二个tap是什么? - alekhine
1个回答

7
一个atan计算永远不会完全正确。而且你让iOS再次根据角度计算cos和sin(cgaffinetransformRotate也是这样做的)。因此,你正在累积三角不准确性。并且由于你仅计算与上一个角度的差异,我想象你还在多个触摸事件中积累不准确性。
如果要旋转某些东西,那么没有理由使用三角学或角度。您可以完全使用线性代数来实现此功能。类似于这样:
v.x = touch.x - center.x; v.y = touch.y - center.y;
float length = sqrt(v.x*v.x + v.y* v.y);
if (length < 5 ) break;
v.x /= length; v.y /= length;

// the rotation matrix is
//  v.x -v.y
//  v.y  v.x

rot.x = previous.x * v.x + previous.y * v.y;
rot.y = previous.x * v.y - previous.y * v.x;

CGAffineTransform rotate = CGAffineTransformMake( rot.x, rot.y, -rot.y, rot.x, 0,0);
...
[view SetTransform:CGAffineTransformConcat ( current, rotate )];
...

previous.x = v.x;
previous.y = v.y;

这段伪代码计算了当前点的标准化向量(前一个点的向量也被存储了下来)。然后我们定义了一个矩阵,将前一个向量旋转到新向量。该矩阵与现有变换进行拼接。
如果旋转不总是从先前状态计算而是从初始状态计算,那么效果会更好。诀窍是仅在触摸时计算“previous”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接