Lele Griccioli
Lele Griccioli

Reputation: 1

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. 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.

So if anyone could help my out it would be much appreciated!

Thank you.

Upvotes: 0

Views: 1244

Answers (1)

k_g
k_g

Reputation: 4464

You have a number of problems with your code.

As @kuroineko mentioned in comments:

atan2 takes the y component first. If you're passing the parameters in the wrong order, you're computing the angle rotated by 90°. Besides, your angle is a float, so using %d in your sprintf will output garbage.

Upvotes: 1

Related Questions