ios - 使用 3 个 CGPoints 寻找角度

标签 ios objective-c cocoa-touch math drawrect

在我的应用程序中,用户点击 3 次,点击的 3 个点将创建一个角度。它完美地绘制了角度。我正在尝试计算第二次点击时的角度,但我认为我做错了(可能是数学错误)。我还没有在我的微积分课上讲过这个,所以我将使用维基百科上的公式。

http://en.wikipedia.org/wiki/Law_of_cosines

这是我正在尝试的:

注意:First、Second 和 Third 是在用户点击时创建的 CGPoints

        CGFloat xDistA = (second.x - third.x);
        CGFloat yDistA = (second.y - third.y);
        CGFloat a = sqrt((xDistA * xDistA) + (yDistA * yDistA));

        CGFloat xDistB = (first.x - third.x);
        CGFloat yDistB = (first.y - third.y);
        CGFloat b = sqrt((xDistB * xDistB) + (yDistB * yDistB));

        CGFloat xDistC = (second.x - first.x);
        CGFloat yDistC = (second.y - first.y);
        CGFloat c = sqrt((xDistC * xDistC) + (yDistC * yDistC));

        CGFloat angle = acos(((a*a)+(b*b)-(c*c))/((2*(a)*(b))));

        NSLog(@"FULL ANGLE IS: %f, ANGLE IS: %.2f",angle, angle);

有时,它给出的角度为 1,这对我来说没有意义。谁能解释这是为什么,或者如何解决?

最佳答案

不确定这是否是主要问题,但确实是个问题

你的答案给出了错误点的角度:

enter image description here

要获得绿色角度(根据变量名称“first”、“second”和“third”,这可能是您想要的角度),请使用:

CGFloat angle = acos(((a*a)+(c*c)-(b*b))/((2*(a)*(c))));

关于ios - 使用 3 个 CGPoints 寻找角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20226804/

相关文章:

ios - 为什么AVCaptureSession Preset High的画面这么黑?

iphone - 如何在iOS的ViewController之间传递对象?

iphone - 在 ViewController 中设置自定义 UITableViewCell 的 UILabel.text

ios - 使用segmentcontroller改变tableview

android - 使用 Google 助理 SDK

iphone - iPad : UIPageViewController navigationOrientation

objective-c - 如何显示来自 NSMutableDictionary 的 NSMutableArray 的动态值列表?

ios - 如何在缩放后校准 Sprite 触摸位置

objective-c - --deep 标志有什么作用?

ios - 将 UITableViewCell 放在 sibling 下面