gpt4 book ai didi

iphone - CGAffineTranformRotate atan2 不准确

转载 作者:可可西里 更新时间:2023-11-01 05:24:24 24 4
gpt4 key购买 nike

我正在转换我的 View ,但总是会发生一些奇怪的事情,我的意思是我使用以下代码计算角度:angle = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);

并且 View 旋转但不正确。我的意思是它以正确的方向旋转,但角度总是不准确,约为 +/- 0.05 弧度。当我再次点击时, View 会旋转到正确的位置。有什么建议吗?将角度精确到逗号后的 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 的“轮子”看起来很相似,但我使用的是自定义绘图。

所以 Circle 是我们旋转的 UIView。 Circle 有 subview - CircleThumbs。 thumb 代表圆的单个部分。点计算正确但我不会解释为什么,因为没有必要。

最佳答案

阿坦计算永远不会完全正确。然后你让 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;

此伪代码计算当前点的归一化向量(也存储了前一个点的向量)。然后我们定义一个矩阵,将先前的向量旋转到新的向量中。该矩阵与现有转换连接在一起。

如果旋转不是总是从前一个状态计算,而是从初始状态计算,那就更好了。诀窍是只为触地得分计算“先前”。

关于iphone - CGAffineTranformRotate atan2 不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8766128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com