找到3个CGPoints的角度 [英] Finding an angle with 3 CGPoints

查看:90
本文介绍了找到3个CGPoints的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application, a user taps 3 times and an angle will be created by the 3 points that were tapped. It draws the angle perfectly. I am trying to calculate the angle at the second tap, but I think I am doing it wrong (probably a math error). I haven't covered this in my calculus class yet, so I am going off of a formula on wikipedia.

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

这就是我的意思尝试:

注意:第一,第二和第三是 CGPoints 在用户点击时创建。

Note: First, Second, and Third are CGPoints created at the user's tap.

        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,这对我来说没有意义。谁能解释为什么会这样,或者如何解决它?

Sometimes, it gives the angle as 1 which doesn't make sense to me. Can anyone explain why this is, or how to fix it please?

推荐答案

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

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

Your answer gives the angle at the wrong point:

< img src =https://i.stack.imgur.com/nRyVY.pngalt =在此处输入图像说明>

获取绿色角度(根据您的变量名称first,second和third,这可能是你想要的角度,使用:

To get the angle in green (which is probably angle you want based on your variable names "first", "second" and "third), use:

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

这篇关于找到3个CGPoints的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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