简体   繁体   English

在C ++中计算两点之间的角度,得出奇怪的结果

[英]Calculating angle between two points in C++, getting weird results

I am trying to calculate the angle between two points using OpenCV C++ in visual studio 2013. One point is a tracked object and the other is the position where i clicked my mouse. 我试图在Visual Studio 2013中使用OpenCV C ++计算两点之间的角度。一个点是跟踪的对象,另一个是我单击鼠标的位置。 However with my code i am getting a very long and fluctuating answer that does not seem correct... 但是,使用我的代码,我得到了一个很长且波动的答案,似乎并不正确...

Code extract: 代码提取:

float angle = (atan2(mousex - posX, mousey - posY) * 180) / 3.14159265;
                sprintf_s(textA, "%d", angle);
                putText(imgOriginal, textA, Point(posX, posY + 20),
    FONT_HERSHEY_COMPLEX_SMALL, 0.8, Scalar(200, 200, 250), 1, CV_AA);

I have included and but still no result. 我包括了,但仍然没有结果。

I get results that fluctuate (while in the same spot) between 50368 and 10732. 我得到的结果在50368和10732之间波动(在同一位置)。

So if anyone could help my out it would be much appreciated! 因此,如果有人可以帮助我,将不胜感激!

Thank you. 谢谢。

You have a number of problems with your code. 您的代码有很多问题。

As @kuroineko mentioned in comments: 正如@kuroineko在评论中提到的:

atan2 takes the y component first. atan2首先使用y分量。 If you're passing the parameters in the wrong order, you're computing the angle rotated by 90°. 如果您以错误的顺序传递参数,则您正在计算旋转90°的角度。 Besides, your angle is a float , so using %d in your sprintf will output garbage. 此外,您的角度是float ,因此在sprintf使用%d将输出垃圾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM