帮助正确计算 atan2 [英] help to calculate atan2 properly

查看:85
本文介绍了帮助正确计算 atan2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算线之间的角度.我需要计算atan.所以我正在使用这样的代码

I need to calculate the angle between lines. I need to calculate atan. So I am using such code

static inline CGFloat angleBetweenLinesInRadians2(CGPoint line1Start, CGPoint line1End) 
{
    CGFloat dx = 0, dy = 0;

    dx = line1End.x - line1Start.x;
    dy = line1End.y - line1Start.y;
    NSLog(@"\ndx = %f\ndy = %f", dx, dy);

    CGFloat rads = fabs(atan2(dy, dx));

    return rads;
}

但我不能超过 180 度((179 度后变为 178..160..150,依此类推.

But I can't get over 180 degrees(( After 179 deg going 178..160..150 and so on.

我需要 360 度旋转.我该怎么做?怎么了?

I need to rotate on 360 degrees. How can I do it? What's wrong?

可能会有所帮助:

//Tells the receiver when one or more fingers associated with an event move within a view or window.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSArray *Touches = [touches allObjects];
    UITouch *first = [Touches objectAtIndex:0];

    CGPoint b = [first previousLocationInView:[self imgView]]; //prewious position
    CGPoint c = [first locationInView:[self imgView]];          //current position

    CGFloat rad1 = angleBetweenLinesInRadians2(center, b);  //first angel
    CGFloat rad2 = angleBetweenLinesInRadians2(center, c);  //second angel

    CGFloat radAngle = fabs(rad2 - rad1);           //angel between two lines
    if (tempCount <= gradus)
    {
        [imgView setTransform: CGAffineTransformRotate([imgView transform], radAngle)];
        tempCount += radAngle;
    }

}

推荐答案

删除 fabs 调用并简单地进行:

Remove the fabs call and simply make it:

CGFloat rads = atan2(dy, dx);

这篇关于帮助正确计算 atan2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆